java:线程的等待和唤醒

Java多线程中的wait与notify   
Java 多线程编程中, wait() 的作用的是让当前线程进入阻塞状态, notify() 是让当前线程唤醒继续执行。虽然是对线程状态的控制,但它们其实都是 Object 中的方法,这是因为 wait notify 所起的作用与线程间的对象锁有关。
在执行 wait() notify() 之前,必须要先获得对象锁 ,即一定要和 synchronized 一起使用。 wait() 的含义是让出获得的对象锁,并让自己进入阻塞状态 。在 notify() 的时候也已经获得了对象锁, 所做的事情就是唤醒当前线程继续执行。
假如 synchronized 的锁对象是 obj 的话, wait notify 正确的使用方法是 obj.wait() obj.notify() 。如果使用 this 作为锁,则可以直接写成 wait() notify() 如果前后使用的锁对象不一致,会发生 IllegalMonitorStateException
当有多个线程共同使用一个对象锁时, notify() 会随机选取一个执行过 wait() 的线程唤醒,其余会继续保持阻塞状态。如果想唤醒所有阻塞的进程,就使用到了 notifyAll()
 
案例:
采用 Java 多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪膛)进行操作,其最大容量是 6 颗子弹。生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹。
public   class  Test {
public   static   void  main(String[] args) {
Gun gun = new  Gun();
Producter pro = new  Producter(gun);
Consumer con = new  Consumer(gun);
new  Thread(pro).start();
new  Thread(con).start();
}
}
 
//
class  Gun {
private   int   maxBulletNum  = 6; // 枪支最大子弹容量
private   int   residueBullet  = 0; // 枪内剩余的子弹数
 
public   synchronized   void  pushBullet() { // 压入子弹
if  ( residueBullet  == maxBulletNum ) { // 如果子弹满了,就等待
try  {
this .wait();
} catch  (InterruptedException e) {
e.printStackTrace();
}
}
System. out .println( " 压入了一个子弹,枪内剩余子弹: "  + ++ residueBullet );
this .notify(); // 唤醒等待的线程
}
 
public   synchronized   void  shoot() { // 射击
if  ( residueBullet  == 0) { // 没子弹了,就等待
try  {
this .wait();
} catch  (InterruptedException e) {
e.printStackTrace();
}
}
System. out .println( " 射击一次,枪内剩余子弹: "  + -- residueBullet );
this .notify(); // 唤醒等待的线程
}
}
 
// 消费者
class  Consumer implements  Runnable {
private  Gun gun  = null ;
 
public  Consumer(Gun gun) {
this . gun  = gun;
}
 
public   void  run() {
while ( true ) {
gun .shoot();
try  {
Thread. sleep (2000);
} catch  (InterruptedException e) {
// TODO  Auto-generated catch block
e.printStackTrace();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值