By GokiSoft.com|
15:20 30/03/2020|
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
Hướng dẫn chữa bài tập
/*
* 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.lession5.bt02;
/**
*
* @author Diep.Tran
*/
public class SharedData {
int rad;
int total;
//index = 1, 2, 3
//1 => Thread 1
//2 => Thread 2
//3 => Thread 3
int index;
public SharedData() {
total = 0;
index = 1;
}
public int getRad() {
return rad;
}
public void setRad(int rad) {
this.rad = rad;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public synchronized void plus(int value) {
total += value;
}
public synchronized boolean checkAvaiable() {
return total < 2000;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
/*
* 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.lession5.bt02;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread1 extends Thread{
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
Random random = new Random();
while(sharedData.checkAvaiable()) {
synchronized(sharedData) {
int rad = random.nextInt(100) + 1;
sharedData.setRad(rad);
sharedData.plus(rad);
System.out.println("T1 >> " + rad);
//thiet lap luong tiep theo dc phep chay
if(rad % 3 == 0) {
sharedData.setIndex(2);
} else {
sharedData.setIndex(3);
}
//sync thread
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("T1 Stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
/*
* 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.lession5.bt02;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread2 extends Thread{
SharedData sharedData;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
while (sharedData.checkAvaiable()) {
synchronized(sharedData) {
sharedData.notifyAll();
try {
while(sharedData.getIndex() != 2 && sharedData.checkAvaiable()) {
sharedData.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = sharedData.getRad();
if(rad % 3 == 0) {
rad *=rad;
System.out.println("T2 >> " + rad);
}
sharedData.setIndex(1);
}
}
System.out.println("T2 Stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
/*
* 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.lession5.bt02;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread3 extends Thread{
SharedData sharedData;
public Thread3(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
while (sharedData.checkAvaiable()) {
synchronized(sharedData) {
sharedData.notifyAll();
try {
while(sharedData.getIndex() != 3 && sharedData.checkAvaiable()) {
sharedData.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = sharedData.getRad();
if(rad % 2 == 0) {
if(rad % 4 == 0) {
System.out.println("So ngau nhien chia het cho 4");
} else {
System.out.println("So ngau nhieu ko chia het cho 4");
}
} else {
System.out.println("Ko le");
}
sharedData.setIndex(1);
}
}
System.out.println("T3 Stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
/*
* 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.lession5.bt02;
/**
*
* @author Diep.Tran
*/
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();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)