一说到线程通信我们就会想到生产者和消费者,哲学家就餐问题,下面例子是以生产者与消费者来实现线程之间的通信
生产者进程类
消费者进程类
写个测试类
wait与notify方法都是定义在Object方法中,而且是final的,因此会被所有的java类继承但是不能重写,这两个方法的调用必须在synchronized方法或者synchronized块中,而且当前线程要已经拿到了对象锁,当线程执行wait方法时,它会释放对象锁
package com.lamp.test;
public class Cake {
private int number = 0;
public synchronized void increase(){
while(0 != number){ //如果只有一个生产者进程和一个消费者进程,我们还可以用if来判断,但是涉及到2个以上进程就需用while来判断了这也是实践中用的最多的
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++; //假定资源的数量只能为0或者1,为0时加1并唤醒消费者进程,消费者进程相反
System.out.println(number);
this.notify();
}
public synchronized void decrease(){
while(0 == number){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number--;
System.out.println(number);
this.notify();
}
}
生产者进程类
package com.lamp.test;
public class Producer extends Thread {
private Cake cake;
public Producer(Cake cake) {
this.cake = cake;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.cake.increase();
}
}
}
消费者进程类
package com.lamp.test;
public class Consumer extends Thread{
private Cake cake = null;
public Consumer(Cake cake) {
this.cake = cake;
}
@Override
public void run(){
for(int i=0; i<10; i++){
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.cake.decrease();
}
}
}
写个测试类
package com.lamp.test;
public class CakeTest {
public static void main(String[] args) {
Cake cake = new Cake();
Thread t1 = new Producer(cake);
Thread t2 = new Consumer(cake); //确保访问的是同一资源
Thread t3 = new Producer(cake);
Thread t4 = new Consumer(cake);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
wait与notify方法都是定义在Object方法中,而且是final的,因此会被所有的java类继承但是不能重写,这两个方法的调用必须在synchronized方法或者synchronized块中,而且当前线程要已经拿到了对象锁,当线程执行wait方法时,它会释放对象锁