By GokiSoft.com| 20:04 21/08/2021|
Java Advanced

[Share Code] Đồng bộ 3 Thread - Lập trình Java nâng cao - C2010L

#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 java2.lesson05;

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

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

    @Override
    public void run() {
        SharedData sharedData = SharedData.getInstance();
        
        while (sharedData.isLive()) {
            synchronized (sharedData) {
                sharedData.notifyAll();
                try {
                    while(sharedData.isLive() && sharedData.getCurrentThread() != SharedData.THREAD_2) {
                        sharedData.wait();
                    }
                    if(!sharedData.isLive()) break;
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                int rad = sharedData.getRad();
                System.out.println("BP: " + rad * rad);
                
                sharedData.setCurrentThread(SharedData.THREAD_1);
            }
        }
        
        System.out.println("T2 stop");
        synchronized(SharedData.getInstance()) {
            SharedData.getInstance().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 java2.lesson05;

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

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

    @Override
    public void run() {
        SharedData sharedData = SharedData.getInstance();
        while (sharedData.isLive()) {
            synchronized (sharedData) {
                sharedData.notifyAll();
                try {
                    while(sharedData.isLive() && sharedData.getCurrentThread() != SharedData.THREAD_3) {
                        sharedData.wait();
                    }
                    if(!sharedData.isLive()) break;
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                int rad = sharedData.getRad();
                System.out.print("UC: ");
                for (int j = 1; j < rad; j++) {
                    if (rad % j == 0) {
                        System.out.print(j + ", ");
                    }
                }
                System.out.print(rad + "\n");
                
                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 java2.lesson05;

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

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

    @Override
    public void run() {
        SharedData sharedData = SharedData.getInstance();
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            synchronized (sharedData) {
                int rad = random.nextInt(100);
                SharedData.getInstance().setRad(rad);

                System.out.println(rad);
                
                if(rad % 2 == 0) {
                    sharedData.setCurrentThread(SharedData.THREAD_2);
                } else {
                    sharedData.setCurrentThread(SharedData.THREAD_3);
                }
                
                sharedData.notifyAll();
                try {
                    while(sharedData.getCurrentThread() != SharedData.THREAD_1) {
                        sharedData.wait();
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        sharedData.setLive(false);
        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 java2.lesson05;

/**
 * singleton
 * @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 currentThread = THREAD_1;
    boolean live = true;
    
    private static SharedData instance = null;
    
    private SharedData() {
    }
    
    public synchronized static SharedData getInstance() {
        if(instance == null) {
            instance = new SharedData();
        }
        return instance;
    }

    public int getRad() {
        return rad;
    }

    public void setRad(int rad) {
        this.rad = rad;
    }

    public int getCurrentThread() {
        return currentThread;
    }

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

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }
}


#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 java2.lesson05;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        ThreadOne t1 = new ThreadOne();
        ThreadTwo t2 = new ThreadTwo();
        ThreadThree t3 = new ThreadThree();
        
        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)