By GokiSoft.com| 09:25 14/09/2021|
Java Advanced

[Share Code] MultiThread - Synchronized trong Java - Đồng bộ 3 threads - Sinh số ngẫu nhiên, bình phương, và chia hết - C200G

MultiThread - Synchronized trong Java - Đồng bộ 3 threads - Sinh số ngẫu nhiên, bình phương, và chia hết


#ThreadTwo.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class ThreadTwo extends Thread{
    SharedData sharedData;

    public ThreadTwo(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        while(sharedData.isRunning()) {
            synchronized(sharedData) {
                sharedData.notifyAll();
                try {
                    while(sharedData.isRunning() && sharedData.getCurrentThread() != SharedData.THREAD_2) {
                        sharedData.wait();
                    }
                    if(!sharedData.isRunning() && sharedData.getCurrentThread() != SharedData.THREAD_2) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                
                int rad = sharedData.getRad();
                System.out.println("BP: " + rad*rad);
                
                sharedData.setCurrentThread(SharedData.THREAD_1);
            }
        }
        
        System.out.println("T2 stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
}


#ThreadThree.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class ThreadThree extends Thread{
    SharedData sharedData;

    public ThreadThree(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        while(sharedData.isRunning()) {
            synchronized(sharedData) {
                sharedData.notifyAll();
                try {
                    while(sharedData.isRunning() && sharedData.getCurrentThread() != SharedData.THREAD_3) {
                        sharedData.wait();
                    }
                    if(!sharedData.isRunning() && sharedData.getCurrentThread() != SharedData.THREAD_3) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                
                int rad = sharedData.getRad();
                if(rad % 4 == 0) {
                    System.out.println(rad + " chia het cho 4");
                } else {
                    System.out.println(rad + " ko chia het cho 4");
                }
                sharedData.setCurrentThread(SharedData.THREAD_1);
            }
        }
        
        System.out.println("T3 stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
}


#ThreadOne.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class ThreadOne extends Thread{
    SharedData sharedData;

    public ThreadOne(SharedData sharedData) {
        this.sharedData = sharedData;
    }
    
    @Override
    public void run() {
        Random random = new Random();
        
        while(sharedData.isRunning()) {
            synchronized(sharedData) {
                int rad = random.nextInt(20) + 1;
                System.out.println(rad);

                sharedData.setRad(rad);
                if(rad % 3 == 0) {
                    sharedData.setCurrentThread(SharedData.THREAD_2);
                } else {
                    sharedData.setCurrentThread(SharedData.THREAD_3);
                }
                
                sharedData.notifyAll();
                try {
                    while(sharedData.isRunning() && sharedData.getCurrentThread() != SharedData.THREAD_1) {
                        sharedData.wait();
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        System.out.println("T1 stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
    
}


#SharedData.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class SharedData {
    public static final int THREAD_1 = 1;
    public static final int THREAD_2 = 2;
    public static final int THREAD_3 = 3;
    
    int rad;
    int total;
    int currentThread = THREAD_1;
    
    public SharedData() {
        total = 0;
    }

    public int getRad() {
        return rad;
    }

    public void setRad(int rad) {
        this.rad = rad;
        total += rad;
    }
    
    public boolean isRunning() {
        return total <= 200;
    }

    public int getCurrentThread() {
        return currentThread;
    }

    public void setCurrentThread(int currentThread) {
        this.currentThread = currentThread;
    }
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        
        ThreadOne t1 = new ThreadOne(sharedData);
        ThreadTwo t2 = new ThreadTwo(sharedData);
        ThreadThree t3 = new ThreadThree(sharedData);
        
        t1.start();
        t2.start();
        t3.start();
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

GokiSoft.com [Teacher]
GokiSoft.com

2021-09-14 02:47:50


#ThreadTwo.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class ThreadTwo extends SyncThread{
    SharedData sharedData;

    public ThreadTwo(SharedData sharedData) {
        super(sharedData, THREAD_2);
        this.sharedData = sharedData;
    }

    @Override
    public void onRunning() {
        while(sharedData.isRunning()) {
            synchronized(sharedData) {
                if(!checkRunning()) {
                    break;
                }
                
                int rad = sharedData.getRad();
                System.out.println("BP: " + rad*rad);
                
                setCurrentThread(THREAD_1);
            }
        }
    }
}


#ThreadThree.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class ThreadThree extends SyncThread{
    SharedData sharedData;

    public ThreadThree(SharedData sharedData) {
        super(sharedData, THREAD_3);
        this.sharedData = sharedData;
    }

    @Override
    public void onRunning() {
        while(sharedData.isRunning()) {
            synchronized(sharedData) {
                if(!checkRunning()) {
                    break;
                }
                
                int rad = sharedData.getRad();
                if(rad % 4 == 0) {
                    System.out.println(rad + " chia het cho 4");
                } else {
                    System.out.println(rad + " ko chia het cho 4");
                }
                setCurrentThread(THREAD_1);
            }
        }
    }
}


#ThreadOne.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

import java.util.Random;

/**
 *
 * @author Diep.Tran
 */
public class ThreadOne extends SyncThread{
    SharedData sharedData;

    public ThreadOne(SharedData sharedData) {
        super(sharedData, THREAD_1);
        this.sharedData = sharedData;
    }
    
    @Override
    public void onRunning() {
        Random random = new Random();
        
        while(sharedData.isRunning()) {
            synchronized(sharedData) {
                int rad = random.nextInt(20) + 1;
                System.out.println(rad);

                sharedData.setRad(rad);
                if(rad % 3 == 0) {
                    setCurrentThread(THREAD_2);
                } else {
                    setCurrentThread(THREAD_3);
                }
                
                checkRunning();
            }
        }
    }
}


#SyncThread.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public abstract class SyncThread extends Thread {
    public static final int THREAD_1 = 1;
    public static final int THREAD_2 = 2;
    public static final int THREAD_3 = 3;
    
    public static int currentThread = THREAD_1;
    
    SharedData sharedData;
    int id;

    public SyncThread(SharedData sharedData, int id) {
        this.sharedData = sharedData;
        this.id = id;
    }
    
    @Override
    public void run() {
        onRunning();
        
        System.out.println("T" + id + " stop");
        synchronized(sharedData) {
            sharedData.notifyAll();
        }
    }
    
    public abstract void onRunning();
    
    /**
     * Bat buoc dat trong sync.
     * @return 
     */
    protected boolean checkRunning() {
        sharedData.notifyAll();
        try {
            while(sharedData.isRunning() && currentThread != id) {
                sharedData.wait();
            }
            if(!sharedData.isRunning() && currentThread != id) {
                return false;
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        return true;
    }
    
    protected static void setCurrentThread(int id) {
        currentThread = id;
    }
}


#SharedData.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class SharedData {
    int rad;
    int total;
    
    public SharedData() {
        total = 0;
    }

    public int getRad() {
        return rad;
    }

    public void setRad(int rad) {
        this.rad = rad;
        total += rad;
    }
    
    public boolean isRunning() {
        return total <= 200;
    }
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06.bt1102;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        
        ThreadOne t1 = new ThreadOne(sharedData);
        ThreadTwo t2 = new ThreadTwo(sharedData);
        ThreadThree t3 = new ThreadThree(sharedData);
        
        t1.start();
        t2.start();
        t3.start();
    }
}