📄 reader.java
字号:
//Reader.javapackage com.wrox.syncserver;import java.util.Vector;public class Reader extends Thread { // The queue is the communication channel between // this reader and the writer. private Vector queue; // A name so we can tell the readers apart. private String name; /** * Create a reader to read objects from a queue. * @param queue Contains the flow of objects from the writer. * @param name A name so we can distinguish readers. */ public Reader(Vector queue, String name) { this.queue = queue; this.name = name; } public void run() { for (; ; ) { synchronized (queue) { // acquire the monitor while (queue.isEmpty()) { try { queue.wait(); // releases the monitor } catch (InterruptedException ex) { // Nothing to do here. If the queue is empty, // might as well go back to waiting. } } // At this point the monitor has been re-acquired Object o = queue.remove(0); System.out.println(name + " got job number: " + o); } // release the monitor at the end of the synchronized block } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -