答案:CubbyHole ExampleThe class CubbyHole (see Listing 8.9) simulates a cubbyhole. A cubbyhole is a slot that can have only one item in it at a time. One thread puts an item into the slot and another thread takes it out. If a thread tries to put an item into a cubbyhole that is already occupied, the thread blocks until the slot is available. If a thread tries to remove an item from an empty cubbyhole, the thread blocks until an item is added. In this example, the slot is a reference to an object. This technique allows objects to be handed off from one thread to another in a thread-safe manner.CubbyHole是指一个通道,某一时刻只能容纳一个东西,一个线程放,另一个线程取。如果已经有东西,不能再放,直到东西被取出。如果没有东西,则不能取,直到放进去新东西。下面给出一个线程安全的解决方案:CubbyHole.java/** Created on 2005-7-14** Java Thread Programming - Paul Hyde* Copyright ? 1999 Sams Publishing* Jonathan Q. Bo 学习笔记**/package org.tju.msnrl.jonathan.thread.chapter8;/*** @author Jonathan Q. Bo from TJU MSNRL** Email:jonathan.q.bo@gmail.com* Blog:blog.csdn.net/jonathan_q_bo* blog.yesky.net/jonathanundersun** Enjoy Life with Sun!**/public class CubbyHole {private Object obj;public CubbyHole() {obj = null;}public synchronized void putIn(Object obj) throws InterruptedException{print("putIn() ... begin");while(this.obj != null){//等待,直到数据被取出print("putIn() wait ... begin");wait();print("putIn() wait ... end");}this.obj = obj;//添加新对象notifyAll();//通知其它线程:有对象可取print("putIn() ... end");}public synchronized Object takeOut()throws InterruptedException{print("takeOut() ... begin");while(obj == null){//等待,直到有数据可以取print("takeOut() wait ... begin");wait();print("takeOut() wait ... end");}Object temp = obj;obj = null;//将原数据清空notifyAll();//通知其它线程:数据已取出,可填新数据print("takeOut() ... end");return temp;}public void print(String msg){String temp = Thread.currentThread().getName();System.out.println(temp + " - " + msg);}}CubbyHoleMain.java/** Created on 2005-7-14** Java Thread Programming - Paul Hyde* Copyright ? 1999 Sams Publishing* Jonathan Q. Bo 学习笔记**/package org.tju.msnrl.jonathan.thread.chapter8;/*** @author Jonathan Q. Bo from TJU MSNRL** Email:jonathan.q.bo@gmail.com* Blog:blog.csdn.net/jonathan_q_bo* blog.yesky.net/jonathanundersun** Enjoy Life with Sun!**/public class CubbyHoleMain {public static void print(String msg){String temp = Thread.currentThread().getName();System.out.println(temp + " - " + msg);}public static void main(String[] args) {final CubbyHole ch = new CubbyHole();Runnable runA = new Runnable(){public void run(){try{Thread.sleep(500);//故意晚一些执行,让其它线程先取数据String str;str="multithreads";ch.putIn(str);//可一直存print("run() putIn() " + str);str = "programming";ch.putIn(str);print("run() putIn() " + str);str = "with java";ch.putIn(str);上一个:Java Thread Programming 1.8.4 - Inter-thread Communication
下一个:Java Thread Programming 1.8.2 - Inter-thread Communication
- 更多JSP疑问解答:
- jsp新手求指导,不要笑!
- 如何让一个form提取的值传递给多个jsp?
- DW中,新建的html页面能否有jsp或php代码?
- jsp 如何限制表单,实现只能填写特定的数据。
- jsp 和javabean结合的程序有问题
- 从数据库里取出的数据如何传递到另外的jsp页面中
- 你好,ext嵌入那个jsp页面,是不是还需要加上一些插件啊,不太懂,麻烦你了。
- JSP不能处理所有问题吗?还要来一大堆的TLD,TAG,XML。为JSP 非要 Servlet 不可吗?
- 光标离开时全角转半角在jsp中怎么实现
- jsp 页面 打开 pdf 文件 控制大小 和 工具栏 能发份源码么 谢啦
- jsp页面点保存按钮,运行缓慢,弹出对话框提示
- jsp刷新页面如何不闪屏
- jsp 与html 的交互问题?
- jsp小数显示问题 例如 我在oracle 数据库中查询出来的是 0.01 但是在jsp页面上就显示成 .01 没有前面的0
- jsp中日历控件