By GokiSoft.com|
10:01 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 - Singleton - C200G
#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() {
this.sharedData = SharedData.getInstance();
}
@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() {
this.sharedData = SharedData.getInstance();
}
@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;
/**
*
* @author Diep.Tran
*/
public class ThreadOne extends Thread{
SharedData sharedData;
public ThreadOne() {
this.sharedData = SharedData.getInstance();
}
@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;
/**
* 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 total;
int currentThread = THREAD_1;
private static SharedData instance = null;
private SharedData() {
total = 0;
}
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;
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) {
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)