public class Thread1 implements Runnable {
private WaitNotifyAllCommunication waitNotifyAllCommunication;
public Thread1(WaitNotifyAllCommunication waitNotifyAllCommunication) {
this.waitNotifyAllCommunication = waitNotifyAllCommunication;
}
@Override
public void run() {
waitNotifyAllCommunication.set();
}
}
public class Thread2 implements Runnable {
private WaitNotifyAllCommunication waitNotifyAllCommunication;
public Thread2(WaitNotifyAllCommunication waitNotifyAllCommunication) {
this.waitNotifyAllCommunication = waitNotifyAllCommunication;
}
@Override
public void run() {
waitNotifyAllCommunication.get();
}
}
/**
waitall和notifyall都需要放在synchronized中.
在碰到wait之后,synchronized就被释放了.否则线程就一直等待了.
notify或notifyall之后会加锁
* 二.这里采用的是实现的类然后多线程的例子线程通信
*/
public class WaitNotifyAllCommunication {
private volatile int signal;
public synchronized void set () {
signal = 1;
// notify方法会随机叫醒一个处于wait状态的线程
notifyAll(); // notifyAll叫醒所有的处于wait线程,争夺到时间片的线程只有一个
System.out.println("叫醒线程叫醒之后休眠开始...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized int get () {
System.out.println(Thread.currentThread().getName() + " 方法执行了...");
if(signal != 1) {
try {
wait();
System.out.println("叫醒之后");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " 方法执行完毕...");
return signal;
}
/**
//这个的执行过程很值得体会下.
*
Thread-1 方法执行了...
Thread-2 方法执行了...
Thread-0 方法执行了...
Thread-3 方法执行了...
叫醒线程叫醒之后休眠开始...
叫醒之后
Thread-3 方法执行完毕...
叫醒之后
Thread-0 方法执行完毕...
叫醒之后
Thread-2 方法执行完毕...
叫醒之后
Thread-1 方法执行完毕...
Process finished with exit code 0
*/
public static void main(String[] args) {
WaitNotifyAllCommunication d = new WaitNotifyAllCommunication();
Thread1 t1 = new Thread1(d); //thread1是 set
Thread2 t2 = new Thread2(d); //thread2是 get
new Thread(t2).start();
new Thread(t2).start();
new Thread(t2).start();
new Thread(t2).start();
//这个也可以是线程的休眠.
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(t1).start();
}
}
2.4 wait,notify模式生产者消费者
Warehouse1 工厂(生产/消费机器)
PushTarget 生产者
TakeTarget 消费者
Main 执行函数
public class Warehouse1 {
private int count;
public final int MAX_COUNT = 10;
public synchronized void push () {
while(count >= MAX_COUNT) {
try {
System.out.println(Thread.currentThread().getName() + " 库存数量达到上限,生产者停止生产。");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count ++;
System.out.println(Thread.currentThread().getName() + " 生产者生产,当前库存为:" + count);
notifyAll();
}
public synchronized void take () {
while(count <= 0) {
try {
System.out.println(Thread.currentThread().getName() + " 库存数量为零,消费者等待。");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count --;
System.out.println(Thread.currentThread().getName() + " 消费者消费,当前库存为:" + count);
notifyAll();
}
}
public class PushTarget implements Runnable {
private Producer1 producer1;
public PushTarget(Producer1 producer1) {
this.producer1 = producer1;
}
@Override
public void run() {
while(true) {
producer1.push();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TakeTarget implements Runnable {
private Producer1 producer1;
public TakeTarget(Producer1 producer1) {
this.producer1 = producer1;
}
@Override
public void run() {
while(true) {
producer1.take();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Producer1 producer1 = new Producer1();
PushTarget p = new PushTarget(producer1);
TakeTarget t = new TakeTarget(producer1);
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}