By GokiSoft.com|
15:42 17/07/2023|
Java Advanced
MultiThread - Sinh số ngẫu nhiên và hiển thị bình phương số ngẫu nhiên & Synchronized trong Java
Viết chương trình làm task vu như sau
- Thread thứ nhất thực hiện sinh ngẫu nhiên các số tự nhiêu từ 1 tới 20 - Dừng 2s và chạy vô tận
- Thread thứ 2 thực hiện hiển thị bình phương các số được sinh ra từ Thread 1- Dừng 1s và chạy vô tận
- Thực hiện đồng bộ 2 Thread trên (T1 thực hiện sinh ngẫu nhiên > T2 in ra bình phương > T1 tiếp tục sinh > T2 in ra bình phương > ...)
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
Trương Công Vinh
2020-03-28 10:59:29
/*
* 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 thread1;
/**
*
* @author DELL
*/
public class Mail {
public static void main(String[] args) {
data data = new data();
ThreadSquare ts = new ThreadSquare(data);
Threadrandom tr = new Threadrandom(data);
ts.start();
tr.start();
}
}
/*
* 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 thread1;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author DELL
*/
public class ThreadSquare extends Thread {
data data ;
public ThreadSquare(data data) {
this.data = data;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Threadrandom.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; ; i++) {
synchronized (data) {
data.notifyAll();
try {
data.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ThreadSquare.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = data.getData();
int Sqr = rad * rad;
System.out.println("Square " + (i + 1) + " : " + Sqr);
}
}
}
}
/*
* 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 thread1;
/**
*
* @author DELL
*/
public class data {
int data;
public data() {
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
/*
* 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 thread1;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author DELL
*/
public class Threadrandom extends Thread{
data data ;
public Threadrandom(data data) {
this.data = data;
}
@Override
public void run() {
Random randem = new Random();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Threadrandom.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0;; i++) {
synchronized(data){
int rad = randem.nextInt(20)+1;
data.setData(rad);
System.out.println("Integer " + (i+1) + " : " + rad);
data.notifyAll();
try {
data.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Threadrandom.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}