By GokiSoft.com| 09:35 20/08/2021|
Java Advanced

[Share Code] Hướng dẫn đồng bộ 3 Thread - Lập trình Java nâng cao.

#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.lesson06;

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

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

    ShareData shareData;

    public ThreadTwo(ShareData shareData) {
        this.shareData = shareData;
    }

    @Override
    public void run() {
        while (shareData.isLive()) {
            synchronized (shareData) {
                try {
                    shareData.notifyAll();
                    while (shareData.isLive() && shareData.currentThread != ShareData.THREAD_2) {
                        shareData.wait();
                    }
                    if (!shareData.isLive()) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadTwo.class.getName()).log(Level.SEVERE, null, ex);
                }

                int rad = shareData.getRad();
                System.out.println("BT: " + (rad * rad));

                shareData.setCurrentThread(ShareData.THREAD_1);
            }
        }
        System.out.println("T2 stop");
        synchronized (shareData) {
            shareData.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.lesson06;

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

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

    ShareData shareData;

    public ThreadThree(ShareData shareData) {
        this.shareData = shareData;
    }

    @Override
    public void run() {
        while (shareData.isLive()) {
            synchronized (shareData) {
                try {
                    shareData.notifyAll();
                    while (shareData.isLive() && shareData.currentThread != ShareData.THREAD_3) {
                        shareData.wait();
                    }
                    if (!shareData.isLive()) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadTwo.class.getName()).log(Level.SEVERE, null, ex);
                }

                int rad = shareData.getRad();
                System.out.println("LT: " + (rad * rad * rad));

                shareData.setCurrentThread(ShareData.THREAD_1);
            }
        }
        System.out.println("T3 stop");
        synchronized (shareData) {
            shareData.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.lesson06;

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

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

    ShareData shareData;

    public ThreadOne(ShareData shareData) {
        this.shareData = shareData;
    }

    @Override
    public void run() {
        Random random = new Random();

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

                System.out.println(rad);

                try {
                    if(rad % 2 == 0) {
                        shareData.setCurrentThread(ShareData.THREAD_2);
                    } else {
                        shareData.setCurrentThread(ShareData.THREAD_3);
                    }
                    shareData.notifyAll();
                    if(shareData.isLive()) {
                        while(shareData.getCurrentThread() != ShareData.THREAD_1) {
                            shareData.wait();//Thread se tam thoi ko chay -> tiep tuc chay -> notify (notifyAll) -> danh thuc Thread -> Tiep tuc chay
                        }
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        shareData.setLive(false);
        System.out.println("T1 stop");
        synchronized(shareData) {
            shareData.notifyAll();
        }
    }
}


#ShareData.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.lesson06;

/**
 *
 * @author Diep.Tran
 */
public class ShareData {
    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;

    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.lesson06;

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