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)
Lê Sỹ Khai
2021-09-11 04:50:11
#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 thread;
/**
*
* @author LE KHAI
*/
public class Main {
public static void main(String[] args) {
ShareData shareData =new ShareData();
ThreadOne t1 = new ThreadOne(shareData);
ThreadTwo t2 = new ThreadTwo(shareData);
t1.start();
t2.start();
}
}
#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 thread;
/**
*
* @author LE KHAI
*/
public class ShareData {
int rad;
public ShareData() {
}
public int getRad() {
return rad;
}
public void setRad(int rad) {
this.rad = rad;
}
}
#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 thread;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author LE KHAI
*/
public class ThreadOne extends Thread{
ShareData shareData;
public ThreadOne(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
}
Random random = new Random();
for (int i = 0; ; i++) {
synchronized(shareData){
int rad = random.nextInt(20);
shareData.setRad(rad);
System.out.println(rad);
shareData.notifyAll();
try {
shareData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ThreadOne.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
#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 thread;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author LE KHAI
*/
public class ThreadTwo extends Thread{
ShareData shareData;
public ThreadTwo(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
for (int i = 0; ; i++) {
synchronized(shareData){
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(ThreadTwo.class.getName()).log(Level.SEVERE, null, ex);
}
shareData.notifyAll();
try {
shareData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(ThreadTwo.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = shareData.getRad();
System.out.println("BP: " +rad*rad);
}
}
}
}
Nguyễn Hùng Anh
2021-09-11 03:51:28
#Main.java
public class Main {
public static void main(String [] args) {
SharedData sharedData = new SharedData();
ThreadOne t1 = new ThreadOne(sharedData);
ThreadTwo t2 = new ThreadTwo(sharedData);
t1.start();
t2.start();
}
}
#SharedData.java
public class SharedData {
int rad;
public int getRad() {
return rad;
}
public void setRad(int rad) {
this.rad = rad;
}
}
#ThreadOne.java
import java.util.Random;
public class ThreadOne extends Thread{
SharedData sharedData;
public ThreadOne(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
Random random = new Random();
do {
synchronized (sharedData) {
int rad =random.nextInt(20);
if (rad == 0) {
continue;
}
System.out.println(rad);
sharedData.setRad(rad);
sharedData.notifyAll();
try {
sharedData.wait(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} while (sharedData.getRad() != 21);
synchronized (sharedData) {
sharedData.notifyAll();
}
}
}
#ThreadTwo.java
public class ThreadTwo extends Thread{
SharedData sharedData = new SharedData();
public ThreadTwo(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
do {
synchronized (sharedData) {
sharedData.notifyAll();
try {
sharedData.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int rad = sharedData.getRad();
System.out.println("BP: " + rad*rad);
}
} while (sharedData.getRad() != 21);
synchronized (sharedData) {
sharedData.notifyAll();
}
}
}
Hieu Ngo
2021-09-11 03:32:29
#Main.java
public class Main {
public static void main(String [] args){
ShareData shareData = new ShareData();
ThreadOne t1 = new ThreadOne(shareData);
ThreadTwo t2 = new ThreadTwo(shareData);
t1.start();
t2.start();
}
}
#ShareData.java
public class ShareData {
int rad;
public ShareData() {
}
public ShareData(int rad) {
this.rad = rad;
}
public int getRad() {
return rad;
}
public void setRad(int rad) {
this.rad = rad;
}
}
#ThreadOne.java
import java.util.Random;
public class ThreadOne extends Thread{
ShareData shareData;
public ThreadOne(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random random = new Random();
for(int i = 0;i < 10;i++) {
synchronized (shareData) {
int rad = random.nextInt(20);
System.out.println("rad = " + rad);
shareData.setRad(rad);
shareData.notifyAll();
try {
shareData.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (shareData){
shareData.notifyAll();
}
}
}
#ThreadTwo.java
public class ThreadTwo extends Thread{
ShareData shareData;
public ThreadTwo(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i =0 ; i< 10;i++) {
synchronized (shareData){
shareData.notifyAll();
try {
shareData.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
int rad = shareData.getRad();
System.out.println("BP =" + rad*rad);
}
}
synchronized (shareData){
shareData.notifyAll();
}
}
}
Đào Mạnh Dũng
2021-08-20 09:56:56
/*
* 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 pkg1101;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Đào Dũng
*/
public class Main {
/**
*/
public static void main(String[] args) {
Ojb ojb = new Ojb();
new Thread(() -> {
while (true) {
synchronized (ojb) {
try {
ojb.notifyAll();
ojb.wait();
int rad = ojb.i;
ojb.i = rad * rad;
System.out.println("T2: " + ojb.i);
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}).start();
new Thread(() -> {
for (;;) {
synchronized (ojb) {
ojb.i=(int) (20*Math.random())+1;
System.out.println("T1: "+ojb.i);
try {
Thread.sleep(1000);
ojb.notifyAll();
ojb.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}).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 pkg1101;
/**
*
* @author Đào Dũng
*/
public class Ojb {
public int i;
}
Triệu Văn Lăng
2021-03-19 08:12:09
/*
* 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 bai1101;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MTLS
*/
public class Thread2 extends Thread {
SharedData sd;
public Thread2(SharedData sd) {
this.sd = sd;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (sd) {
try {
sd.notifyAll();
sd.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int rnd = sd.getRd();
rnd *= rnd;
System.out.println("BP: " + rnd);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
}
synchronized(sd) {
sd.notifyAll();
}
}
}
}
Triệu Văn Lăng
2021-03-19 08:11:51
/*
* 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 bai1101;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MTLS
*/
public class Thread1 extends Thread{
SharedData sd;
public Thread1(SharedData sd) {
this.sd = sd;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
Random rd = new Random();
for (int i = 0; i < 10; i++) {
synchronized(sd) {
int rnd = rd.nextInt(20);
sd.setRd(rnd);
System.out.println("rad: " + rnd);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
sd.notifyAll();
try {
sd.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
Triệu Văn Lăng
2021-03-19 08:11:22
/*
* 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 bai1101;
/**
*
* @author MTLS
*/
class SharedData {
int c = 0;
int rd;
public synchronized void add(String threadName, int deltai) {
System.out.println(threadName + "->" + c);
c +=deltai;
System.out.println(threadName + "->" + "c: " + c);
}
public synchronized void minus(String threadName, int deltai) {
System.out.println(threadName + "->" + c);
c -=deltai;
System.out.println(threadName + "->" + "c: " + c);
}
public int getRd() {
return rd;
}
public void setRd(int rd) {
this.rd = rd;
}
}
Triệu Văn Lăng
2021-03-19 08:11:03
/*
* 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 bai1101;
/**
*
* @author MTLS
*/
public class Main {
public static void main(String[] args) {
SharedData sd = new SharedData();
Thread1 t1 = new Thread1(sd);
Thread2 t2 = new Thread2(sd);
t1.start();
t2.start();
}
}
vuong huu phu
2021-03-18 03:56:24
/*
* 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 javaapplication35;
/**
*
* @author Admin
*/
public class S_dt {
int r ;
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
vuong huu phu
2021-03-18 03:56:02
/*
* 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 javaapplication35;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Admin
*/
public class Thread2 extends Thread {
S_dt s;
public Thread2(S_dt s) {
this.s = s;
}
@Override
public void run() {
for (int i = 0; i > -1; i++) {
synchronized(s){
try {
s.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
s.notifyAll();
int n = s.getR();
n *= n;
System.out.println("T2 = " + n);
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}}
}
}