exercise19_8.java
来自「java程序设计导论(daniel liang著) 所有偶数课后习题答案」· Java 代码 · 共 47 行
JAVA
47 行
// Exercise19_8.java: Define threads using the Thread classimport java.util.*;public class Exercise19_8 { /** Main method */ public static void main(String[] args) { new Exercise19_8(); } public Exercise19_8() { // Start threads new Producer().start(); new Consumer().start(); } private Stack stack = new Stack(); private int i = 0; class Producer extends Thread { public void run() { while (true) { System.out.println("Producer: put " + i); stack.push(new Integer(i++)); synchronized (stack) { stack.notifyAll(); } } } } class Consumer extends Thread { public void run() { while (true) { synchronized (stack) { try { while (stack.isEmpty()) stack.wait(); System.out.println("Consumer: get " + stack.pop()); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?