Java多线程——线程间通信wait、notify、notifyAll
问题提出
问题:账户余额不够了怎么办?
等待存入足够的钱后处理,即我们需要线程间通信。
案例:生产者、消费者问题
生产者生产,消费者存取,消费者在Queue(队列)中为0时不能进行存取
定义Queue类,负责给n赋值和存取
package com.study.myqueue;
public class Queue {
private int n;
public synchronized int get() {
System.out.println("消费:"+n);
return n;
}
public synchronized void set(int n) {
System.out.println("生产:"+n);
this.n = n;
}
}
定义生产者类,使用循环修改n的值
package com.study.queue;
public class Producer implements Runnable{
Queue queue;
Producer(Queue queue){
this.queue=queue;
}
@Override
public void run() {
int i=0;
while(true){
queue.set(i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
定义消费者类
package com.study.queue;
public class Consumer implements Runnable{
Queue queue;
Consumer(Queue queue){
this.queue=queue;
}
@Override
public void run() {
while(true){
queue.get();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试类
package com.study.myqueue;
public class Test {
public static void main(String[] args) {
Queue queue=new Queue();
new Thread(new Producer(queue)).start();
new Thread(new Consumer(queue)).start();
}
}
运行结果:
生产:0
消费:0
生产:1
消费:1
消费:1
生产:2
消费:2
生产:3
消费:3
生产:4
消费:4
生产:5
消费:5
生产:6
通过运行结果发现:在运行结果第5行出现了生产1次,消费2两次的结果。这说明程序有问题。
线程间通信(wait、notify、notifyAll)
线程间通信的方法、线程死锁
(1) wait()方法:中断方法的执行,使线程等待;
注:不能两个线程都处于等待状态,即不能是生产者等消费者消费,消费者等生产者生产,这样就会发生线程死锁。
线程死锁解决方法:notify()方法或者notifyAll()方法。
(2) notify()方法:唤醒处于等待的某一个线程,使其结束等待。
(3) notifyAll()方法:唤醒所有处于等待的线程,使它们结束等待。
修改程序(添加wait方法)
修改思路:在Queue中添加一个boolean类型的flag,将Queue类看做一个容器,在容器中放了一个数据,当flag为false时,表示容器中没有数据,这时就需要我们调用set()方法去生产数据,当生产数据之后,flag的值就会变为true,那么get()方法就可以获取数据。即当flag=false时,不能消费;flag=true时,不能生产。
package com.study.queue;
public class Queue {
private int n;
boolean flag=false;
public synchronized int get() {
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("消费:"+n);
flag=false;//消费完毕,容器中没有数据
return n;
}
public synchronized void set(int n) {
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("生产:"+n);
this.n = n;
flag=true;//生产完毕,容器中已经有数据
}
}
运行结果:
生产:0
消费:0
消费:0
生产:1
生产:2
消费:2
消费:2
生产:3
消费:3
生产:4
消费:4
生产:5
生产:6
消费:6
生产:7
通过结果我们发现,线程进行了一部分进行不下去了,这是因为发生了线程死锁,两个线程都在执行wait()方法。解决方法是添加notify()方法。
修改程序(添加notify方法)
package com.study.queue;
public class Queue {
private int n;
boolean flag=false;
public synchronized int get() {
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("消费:"+n);
flag=false;//消费完毕,容器中没有数据
notifyAll();
return n;
}
public synchronized void set(int n) {
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("生产:"+n);
this.n = n;
flag=true;//生产完毕,容器中已经有数据
notifyAll();
}
}
注:一般不使用notify()方法,因为是随机唤醒,不知道唤醒的是哪一个。我们一般使用notifyAll()方法,将线程全部唤醒。
此时,程序可以正常运行。