日期:2014-05-20 浏览次数:20716 次
package thread;
public class Test {
public static void main(String[] args) {
Queue q=new Queue();
Producer p=new Producer(q);
Consumer c=new Consumer(q);
p.start();
c.start();
}
}
class Producer extends Thread{
Queue q;
public Producer(Queue q) {
this.q=q;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Producer put:"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread{
Queue q;
Consumer(Queue q){
this.q=q;
}
@Override
public void run() {
while (true){
System.out.println("Consumer get:"+q.get());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Queue{
int value;
public void put(int i){
this.value=i;
}
public int get(){
return value;
}
}