?? pcfixed.java
字號:
class Q{
int n;
boolean valueSet=false;
synchronized int get(){
if(!valueSet)
try{
wait();
}
catch(InterruptedException e){
System.out.println("InterruptedException caught.");
}
System.out.println("Got: "+n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n){
if(valueSet)
try{
wait();
}
catch(InterruptedException e){
System.out.println("InterruptedException caugth.");
}
this.n=n;
System.out.println("Put: "+n);
valueSet=true;
notify();
}
}
class Producer implements Runnable{
Q q;
Producer(Q q){
this.q=q;
new Thread(this,"Producer").start();
}
public void run(){
int i=0;
while(true){
q.put(i++);
}
}
}
class Consumer implements Runnable{
Q q;
Consumer(Q q){
this.q=q;
new Thread(this,"Consumer").start();
}
public void run(){
while(true){
q.get();
}
}
}
class PCFixed{
public static void main(String args[]){
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -