By GokiSoft.com|
18:59 22/03/2023|
Java Advanced
[Source Code] MultiThread - Sinh số ngẫu nhiên và hiển thị bình phương số ngẫu nhiên & Synchronized trong Java - C2206L
MultiThread - Sinh số ngẫu nhiên và hiển thị bình phương số ngẫu nhiên & Synchronized trong Java
#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.bt1097;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
//TONG THOI GIAN XU LY: 20S -> STOP
Thread1 t1 = new Thread1(sharedData);
Thread2 t2 = new Thread2(sharedData);
t1.start();
t2.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.bt1097;
/**
*
* @author teacher
*/
public class SharedData {
int rad;
int timeCounter = 0;
public SharedData() {
}
public int getRad() {
return rad;
}
public void setRad(int rad) {
this.rad = rad;
}
public void addTimeMore(int delta) {
timeCounter += delta;
}
public boolean isLive() {
return timeCounter < 20;
}
}
#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.bt1097;
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() {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
Random random = new Random();
synchronized(sharedData) {
while (sharedData.isLive()) {
int rad = random.nextInt(100);
System.out.println("T1 > " + rad);
sharedData.setRad(rad);
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
sharedData.addTimeMore(2);
}
}
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.bt1097;
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 {
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));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
sharedData.addTimeMore(1);
}
}
System.out.println("T2 stop");
synchronized(sharedData) {
sharedData.notifyAll();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)