1.wait,notify概述
- 目的是实现线程通信
- wait和notify都需要放在synchronized(方法和代码块都行)中.
- 在碰到wait之后,synchronized就被释放了.
- notify叫醒一个线程,notifyAll会叫醒多个线程(一个个的进行),
- 可以通过Jconsole来查看wait的线程
2.示例
2.1 在不用wait,notify的情况下实现线程通信
public class SignalCommunication {
private volatile int signal;
public void set (int value) {
this.signal = value;
}
public int get () {
return signal;
}
public static void main(String[] args) {
SignalCommunication d = new SignalCommunication();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("修改状态的线程执行...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
d.set(1);
System.out.println("状态值修改成功。。。");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(d.get() != 1) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("模拟代码的执行...");
}
}).start();
}
}
2.2 通过wait,notify实现线程通信
- 匿名类new thread然后多线程的线程通信(过wait,notifyl)
public class WaitNotifyCommunication {
private volatile int signal;
public void set (int value) {
this.signal = value;
}
public int get () {
return signal;
}
public static void main(String[] args) {
WaitNotifyCommunication waitNotifyCommunication = new WaitNotifyCommunication();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (waitNotifyCommunication) {
System.out.println("修改状态的线程执行...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
waitNotifyCommunication.set(1);
System.out.println("状态值修改成功。。。");
waitNotifyCommunication.notify();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (waitNotifyCommunication) {
while(waitNotifyCommunication.get() != 1) {
try {
waitNotifyCommunication.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("模拟代码的执行...");
}
}
}).start();
}
}
2.3 通过wait,notifyall实现线程通信
- 实现多线程的线程通信(过wait,notifyall)
- Thread1 线程1,Thread2 线程2,WaitNotifyAllCommunication 线程通信,执行函数main
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();
}
}
public class WaitNotifyAllCommunication {
private volatile int signal;
public synchronized void set () {
signal = 1;
notifyAll();
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;
}
public static void main(String[] args) {
WaitNotifyAllCommunication d = new WaitNotifyAllCommunication();
Thread1 t1 = new Thread1(d);
Thread2 t2 = new Thread2(d);
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();
}
}