快速业务通道

Java中使用wait()与notify()实现线程间协作 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-18
ew Game();     for (int i = 0; i < 10; i++)      game.addPlayer(new Athlete(i, game));     new Thread(game).start();    } }

Java中使用wait()与notify()实现线程间协作(2)

时间:2011-03-24 zhangjunhd

结果:

Ready...... Ready...... Ready...... Athlete<0> ready! Athlete<1> ready! Athlete<2> ready! Athlete<3> ready! Athlete<4> ready! Athlete<5> ready! Athlete<6> ready! Athlete<7> ready! Athlete<8> ready! Athlete<9> ready! Go! Athlete<9> go! Athlete<8> go! Athlete<7> go! Athlete<6> go! Athlete<5> go! Athlete<4> go! Athlete<3> go! Athlete<2> go! Athlete<1> go! Athlete<0> go!

3.模拟忙等待过程

MyObject类的实例是被观察者,当观察事件发生时,它会通知 一个Monitor类的实例(通知的方式是改变一个标志位)。而此 Monitor类的实例是通过忙等待来不断的检查标志位是否变化。

BusyWaiting.java

import java.util.concurrent.TimeUnit; class MyObject implements Runnable {    private Monitor monitor;    public MyObject(Monitor monitor) {     this.monitor = monitor;    }    public void run() {     try {      TimeUnit.SECONDS.sleep(3);      System.out.println("i''m going.");      monitor.gotMessage();     } catch (InterruptedException e) {       e.printStackTrace();     }    } } class Monitor implements Runnable {    private volatile boolean go = false;    public void gotMessage() throws InterruptedException {     go = true;    }    public void watching() {     while (go == false)      ;     System.out.println("He has gone.");    }    public void run() {     watching();    } } public class BusyWaiting {    public static void main(String[] args) {     Monitor monitor = new Monitor();     MyObject o = new MyObject(monitor);     new Thread(o).start();     new Thread(monitor).start();    } }

结果:

i''m going. He has gone.

Java中使用wait()与notify()实现线程间协作(3)

时间:2011-03-24 zhangjunhd

4.使用wait()与notify()改写上面的例子

下面的例子通过wait()来取代忙等待机制,当收到通知消息时 ,notify当前Monitor类线程。

Wait.java

package concurrency.wait; import java.util.concurrent.TimeUnit; class MyObject implements Runnable {    private Monitor monitor;    public MyObject(Monitor monitor) {     this.monitor = monitor;    }    public void run() {     try {      TimeUnit.SECONDS.sleep(3);       System.out.println("i''m going.");      monitor.gotMessage();     } catch (InterruptedException e) {      e.printStackTrace();     }    } } class Monitor implements Runnable {    private volatile boolean go = false;    public synchronized void gotMessage() throws InterruptedException {     go = true;     notify();    }    public synchronized void watching() throws InterruptedException {     while (go == false)      wait();     System.out.println("He has gone.");    }  

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号