1 2 3 4 5 6 7 8 9 10 11 12 | class ActiveQueue { ... public synchronized void enqueue(ClientRequest cr) throws InterruptedException { while(isFull( ) ){ // ActiveQueue的大小达到上限 wait(); } // 把用户请求添加到处理队列中 notifyAll(); } ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public final void wait (long millisecTimeout) throws InterruptedException; 使用该方法改进后的代码实现片断如下: class ActiveQueue { ... public synchronized void enqueue(ClientRequest cr, long timeout) throws InterruptedException { while(isFull( ) ){ // ActiveQueue的大小达到上限 wait(timeout); // 语意模糊性体现于此,当wait返回时 // 我们无法区分是由于notify的通知还是超时触发的 // 因此我们无法做出适当的处理 } // 把用户请求添加到处理队列中 notifyAll(); } ... } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |