By GokiSoft.com|
09:40 20/08/2021|
Java Advanced
[Share Code] Sinh số ngẫu nhiên - tính bình phương - Bài toán xử lý đa luồng - Multi Thread trong Java - notifyAll() - wait() - Lập trình Java
Notify, NotifyAll, Wait -> Sync
Bài toán:
T1 -> Sinh ngẫu nhiên các số tự nhiên rad: 0 -> 100
T2 -> Hiển thị BT số rad được sinh từ T1
Yêu cầu sau:
T1...(rad)... ...(rad)... ......
T2. ...(rad*rad)... ...(rad*rad)...
==============================================================
T1(sync) ..(rad)..set..display..notifyAll..wait flag=true ..(rad)..set..display..
T2..(notifyAll)..(wait) flag=true ..get..bt..continue..notifyAll..wait
T1..(rad)..set..display..notifyAll..wait flag=true ..(rad)..set..display..notifyAll..wait
T2 ..notifyAll..wait flag=true ..get..bt....
==============================================================
Bài toán:
T1 -> Sinh ngẫu nhiên các số tự nhiên rad: 0 -> 100
T2 -> rad:chan -> hien thị bt
T3 -> rad:lẻ -> hiển thị lập phường
T1...(rad)... ...(rad)... ...(rad)... ...(rad)...
T2. ...(rad*rad)... ...(rad*rad)...
T3. ...(rad*rad*rad)... ...(rad*rad*rad)...
#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 java2.lesson06;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class ThreadTwo extends Thread {
ShareData shareData;
public ThreadTwo(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (shareData) {
try {
shareData.notifyAll();
shareData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ThreadTwo.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = shareData.getRad();
System.out.println("BT: " + (rad * rad));
}
}
System.out.println("T2 stop");
synchronized(shareData) {
shareData.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 java2.lesson06;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class ThreadOne extends Thread {
ShareData shareData;
public ThreadOne(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
Random random = new Random();
for (int i = 0; i < 10; i++) {
synchronized (shareData) {
int rad = random.nextInt(100);
shareData.setRad(rad);
System.out.println(rad);
try {
shareData.notifyAll();
shareData.wait();//Thread se tam thoi ko chay -> tiep tuc chay -> notify (notifyAll) -> danh thuc Thread -> Tiep tuc chay
} catch (InterruptedException ex) {
Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("T1 stop");
synchronized(shareData) {
shareData.notifyAll();
}
}
}
#ShareData.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 java2.lesson06;
/**
*
* @author Diep.Tran
*/
public class ShareData {
int rad;
public int getRad() {
return rad;
}
public void setRad(int rad) {
this.rad = rad;
}
}
#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 java2.lesson06;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
ShareData shareData = new ShareData();
ThreadOne t1 = new ThreadOne(shareData);
ThreadTwo t2 = new ThreadTwo(shareData);
t2.start();
t1.start();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)