/*
* 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.bt3189;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
Thread1 t1 = new Thread1(sharedData);
Thread2 t2 = new Thread2(sharedData);
t1.start();
t2.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 com.gokisoft.bt3189;
/**
*
* @author teacher
*/
public class SharedData {
int index;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
/*
* 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.bt3189;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread1 extends Thread {
SharedData sharedData;
String[] weeks;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
weeks = new String[]{"Thu Hai", "Thu Ba", "Thu Tu", "Thu Nam", "Thu Sau", "Thu Bay", "Chu Nhat"};
}
@Override
public void run() {
Random random = new Random();
int length = weeks.length - 1;
synchronized (sharedData) {
for (;;) {
int rad = random.nextInt(length);
System.out.println("T1 > " + weeks[rad]);
sharedData.setIndex(rad);
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// System.out.println("T1 stop");
// synchronized(sharedData) {
// sharedData.notifyAll();
// }
}
}
/*
* 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.bt3189;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread2 extends Thread {
SharedData sharedData;
String[] weeks;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
weeks = new String[]{"Monday", "Tuesday", "Wednesday", "Thurday", "Friday", "Saturday", "Sunday"};
}
@Override
public void run() {
synchronized (sharedData) {
for (;;) {
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
int rad = sharedData.getIndex();
System.out.println("T2 > " + weeks[rad]);
}
}
// System.out.println("T2 stop");
// synchronized(sharedData) {
// sharedData.notifyAll();
// }
}
}
Ứng Dụng Học
Theo dõi cập nhật nội dung học trên Youtube & Facebook
Thông Tin Liên Hệ
Công Ty Cổ Phần Phát Triển Công Nghệ Gozic.
Website: https://gozic.vn
SĐT: 096 - 70 25 996
Email: ziczacgroup@gmail.com
Thiết kế webiste chuyên nghiệp
Thiết kế phần mềm quản trị
Thiết kế ứng dụng Android
Thiết kế ứng dụng IOS
Thiết kế Web App
Hỗ trợ Digital Marketing
Hỗ trợ quảng cáo Google Ads
Hỗ trợ quảng cáo Facebook Ads
Hỗ trợ SEO Website