By GokiSoft.com|
20:03 27/07/2022|
Java Advanced
[Source Code] Xử lý Thread & Synchronized Thread trong Java - C2108L
#Thread4.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.lesson05;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread4 extends Thread {
@Override
public void run() {
SharedData sharedData = SharedData.getInstance();
synchronized (sharedData) {
for (int i = 0; i < 10; i++) {
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread4.class.getName()).log(Level.SEVERE, null, ex);
}
int rand = SharedData.getInstance().getRand();
System.out.println("T4 > " + (rand * rand));
}
}
System.out.println("T4 stop");
synchronized (sharedData) {
sharedData.notifyAll();
}
}
}
#Thread3.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.lesson05;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Thread3 extends Thread {
@Override
public void run() {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
SharedData sharedData = SharedData.getInstance();
Random random = new Random();
synchronized (SharedData.getInstance()) {
for (int i = 0; i < 10; i++) {
int rand = random.nextInt(10);
System.out.println("T3 > " + rand);
SharedData.getInstance().setRand(rand);
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread3.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("T3 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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class Thread2 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
SharedData.getInstance().changeX(-i);
}
}
}
#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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class Thread1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
SharedData.getInstance().changeX(i);
}
}
}
#Test.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.lesson05;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
Thread3 t3 = new Thread3();
Thread4 t4 = new Thread4();
t3.start();
t4.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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class SharedData {
int x;
int rand;
private static SharedData instance = null;
private SharedData() {
}
public synchronized static SharedData getInstance() {
if(instance == null) {
instance = new SharedData();
}
return instance;
}
//t1 & t2 -> cung goi toi function
//t1 & t2 -> queue -> xu ly tuan
public synchronized void changeX(int delta) {
System.out.println("x = " + x + ", delta = " + delta);
x+=delta;
System.out.println(">> x = " + x);
}
public int getRand() {
return rand;
}
public void setRand(int rand) {
this.rand = rand;
}
}
#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.lesson05;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)