By GokiSoft.com|
20:42 22/03/2023|
Java Advanced
[Souce Code] MultiThread - Synchronized trong Java - Đồng bộ 3 threads - Sinh số ngẫu nhiên, bình phương, và chia hết - C2206L
#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 com.gokisoft.bt1102;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
Thread1 t1 = new Thread1(sharedData);
Thread2 t2 = new Thread2(sharedData);
Thread3 t3 = new Thread3(sharedData);
t1.start();
t2.start();
t3.start();
}
}
#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 com.gokisoft.bt1102;
/**
*
* @author teacher
*/
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) {
total += rad;
this.rad = rad;
}
public boolean isLive() {
return total < 2000;
}
public int getCurrentThread() {
return currentThread;
}
public void setCurrentThread(int currentThread) {
this.currentThread = currentThread;
}
}
#Thread1.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 com.gokisoft.bt1102;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread1 extends Thread{
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
Random random = new Random();
synchronized(sharedData) {
while (sharedData.isLive()) {
int rad = random.nextInt(100);
System.out.println("T1 > " + rad);
sharedData.setRad(rad);
if(rad % 3 == 0) {
//Thread 2 se chay
sharedData.setCurrentThread(SharedData.THREAD_2);
} else {
//Thread 3 se chay
sharedData.setCurrentThread(SharedData.THREAD_3);
}
sharedData.notifyAll();
try {
while (sharedData.isLive() && sharedData.getCurrentThread() != SharedData.THREAD_1) {
sharedData.wait();
}
if(!sharedData.isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("T1 stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
#Thread2.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 com.gokisoft.bt1102;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread2 extends Thread{
SharedData sharedData;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
synchronized(sharedData) {
while (sharedData.isLive()) {
sharedData.notifyAll();
try {
while (sharedData.isLive() && sharedData.getCurrentThread() != SharedData.THREAD_2) {
sharedData.wait();
}
if(!sharedData.isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = sharedData.getRad();
System.out.println("T2 > " + (rad * rad));
//Thread 1 -> se tiep tuc chay
sharedData.setCurrentThread(SharedData.THREAD_1);
}
}
System.out.println("T2 stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
#Thread3.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 com.gokisoft.bt1102;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread3 extends Thread{
SharedData sharedData;
public Thread3(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
synchronized(sharedData) {
while (sharedData.isLive()) {
sharedData.notifyAll();
try {
while (sharedData.isLive() && sharedData.getCurrentThread() != SharedData.THREAD_3) {
sharedData.wait();
}
if(!sharedData.isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
if(sharedData.getRad() % 4 == 0) {
System.out.println("T3 > " + sharedData.getRad() + " % 4 = 0");
} else {
System.out.println("T3 > " + sharedData.getRad() + " % 4 != 0");
}
//Thread 1 -> se tiep tuc chay
sharedData.setCurrentThread(SharedData.THREAD_1);
}
}
System.out.println("T3 stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
VIET CHUONG TRINH TONG QUAT
#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 com.gokisoft.bt1102;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
Thread1 t1 = new Thread1(sharedData);
Thread2 t2 = new Thread2(sharedData);
Thread3 t3 = new Thread3(sharedData);
t1.start();
t2.start();
t3.start();
}
}
#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 com.gokisoft.bt1102;
/**
*
* @author teacher
*/
public class SharedData {
int rad;
int total;
public SharedData() {
total = 0;
}
public int getRad() {
return rad;
}
public void setRad(int rad) {
total += rad;
this.rad = rad;
}
public boolean isLive() {
return total < 2000;
}
}
#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 com.gokisoft.bt1102;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
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;
private static int currentThread = THREAD_1;
private final int _threadIndex;
private final boolean _isRunningFirst;
private final Object _sharedData;
public SyncThread(Object sharedData, int threadIndex, boolean isRunningFirst) {
this._threadIndex = threadIndex;
this._sharedData = sharedData;
this._isRunningFirst = isRunningFirst;
}
@Override
public void run() {
synchronized(_sharedData) {
while (isLive()) {
if(!_isRunningFirst) {
_sharedData.notifyAll();
try {
while (isLive() && _threadIndex != currentThread) {
_sharedData.wait();
}
if(!isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(SyncThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Logic rieng cua tung Thread
onRunning();
if(_isRunningFirst) {
_sharedData.notifyAll();
try {
while (isLive() && _threadIndex != currentThread) {
_sharedData.wait();
}
if(!isLive()) break;
} catch (InterruptedException ex) {
Logger.getLogger(SyncThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
System.out.println("##T" + _threadIndex + " stop");
synchronized(_sharedData) {
_sharedData.notifyAll();
}
}
public void setCurrentThread(int threadIndex) {
currentThread = threadIndex;
}
public abstract boolean isLive();
public abstract void onRunning();
}
#Thread1.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 com.gokisoft.bt1102;
import java.util.Random;
/**
*
* @author teacher
*/
public class Thread1 extends SyncThread{
SharedData sharedData;
Random random;
public Thread1(SharedData sharedData) {
super(sharedData, THREAD_1, true);
this.sharedData = sharedData;
random = new Random();
}
@Override
public boolean isLive() {
return sharedData.isLive();
}
@Override
public void onRunning() {
int rad = random.nextInt(100);
System.out.println("T1 > " + rad);
sharedData.setRad(rad);
if(rad % 3 == 0) {
//Thread 2 se chay
setCurrentThread(THREAD_2);
} else {
//Thread 3 se chay
setCurrentThread(THREAD_3);
}
}
}
#Thread2.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 com.gokisoft.bt1102;
/**
*
* @author teacher
*/
public class Thread2 extends SyncThread{
SharedData sharedData;
public Thread2(SharedData sharedData) {
super(sharedData, THREAD_2, false);
this.sharedData = sharedData;
}
@Override
public boolean isLive() {
return sharedData.isLive();
}
@Override
public void onRunning() {
int rad = sharedData.getRad();
System.out.println("T2 > " + (rad * rad));
//Thread 1 -> se tiep tuc chay
setCurrentThread(THREAD_1);
}
}
#Thread3.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 com.gokisoft.bt1102;
/**
*
* @author teacher
*/
public class Thread3 extends SyncThread{
SharedData sharedData;
public Thread3(SharedData sharedData) {
super(sharedData, THREAD_3, false);
this.sharedData = sharedData;
}
@Override
public boolean isLive() {
return sharedData.isLive();
}
@Override
public void onRunning() {
if(sharedData.getRad() % 4 == 0) {
System.out.println("T3 > " + sharedData.getRad() + " % 4 = 0");
} else {
System.out.println("T3 > " + sharedData.getRad() + " % 4 != 0");
}
//Thread 1 -> se tiep tuc chay
setCurrentThread(THREAD_1);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)