java线程通信

一说到线程通信我们就会想到生产者和消费者,哲学家就餐问题,下面例子是以生产者与消费者来实现线程之间的通信

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方法时,它会释放对象锁
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值