factory.java
来自「mystack」· Java 代码 · 共 44 行
JAVA
44 行
package com.thread.mystack;
public class Factory {
private char index;
private char[] s = new char[200];
public synchronized char pop() {
while (index == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
index--;
char temp = s[index];
notifyAll();
System.out.println("Consumer" + Thread.currentThread().getName() + ":"
+ (char) ('A' + (int) (Math.random() * 26)));
return temp;
}
public synchronized void push(char i) {
while (index == 200) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
i = s[index];
++index;
notifyAll();
System.out.println("Producer" + Thread.currentThread().getName() + ":"
+ (char) ('A' + (int) (Math.random() * 26)));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?