回顾生产者/消费者问题下产生的java多线程(二)(三)

2014-11-24 02:22:25 · 作者: · 浏览: 6

public static void main(String[] args) throws Exception{
Sycn3 s3 = new Sycn3();
s3.start();
}

class Producer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue.size() == MAX)
System.out.println("warning: it's full!");
Object o = new Object();
queue.put(o);
System.out.println("Producer: " + o);
}catch(InterruptedException e){
System.out.println("producer is interrupted!");
}
//}
}
}
}

class Consumer extends Thread{
public void run(){
while(true){
//synchronized(this){
try{
if(queue.size() == 0)
System.out.println("warning: it's empty!");
Object o = queue.take();
System.out.println("Consumer: " + o);
}catch(InterruptedException e){
System.out.println("producer is interrupted!");
}
//}
}
}
}

}

4. 管道方法PipedInputStream/PipedOutputStream