java多线程模拟生产者消费者问题
2013-04-24 22:42
591 查看
public class ProducerConsumer {
public static void main(String[] args) {
Storage s = new Storage();
Producer p = new Producer(s);
Consumer c = new Consumer(s);
Thread tp = new Thread(p);
Thread tc = new Thread(c);
tp.start();
tc.start();
}
}
class Consumer implements Runnable {//消费者
Storage s = null;
public Consumer(Storage s){
this.s = s;
}
public void run() {
for(int i=0; i<20; i++){
Product p = s.pop();//取出产品
try {
Thread.sleep((int)(Math.random()*1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable {//生产者
Storage s = null;
public Producer(Storage s){
this.s = s;
}
public void run() {
for(int i=0; i<20; i++){
Product p = new Product(i);
s.push(p);
//放入产品
try {
Thread.sleep((int)(Math.random()*1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Product {
int id;
public Product(int id){
this.id = id;
}
public String toString(){//重写toString方法
return "产品:"+this.id;
}
}
class Storage {
int index = 0;
Product[] products = new Product[5];
public synchronized void push(Product p){//放入
while(index==this.products.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.products[index] = p;
System.out.println("生产者放入"+index+"位置:" + p);
index++;
this.notifyAll();
}
public synchronized Product pop(){//取出
while(this.index==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
index--;
this.notifyAll();
System.out.println("消费者从"+ index+ "位置取出:" + this.products[index]);
return this.products[index];
}
}
public static void main(String[] args) {
Storage s = new Storage();
Producer p = new Producer(s);
Consumer c = new Consumer(s);
Thread tp = new Thread(p);
Thread tc = new Thread(c);
tp.start();
tc.start();
}
}
class Consumer implements Runnable {//消费者
Storage s = null;
public Consumer(Storage s){
this.s = s;
}
public void run() {
for(int i=0; i<20; i++){
Product p = s.pop();//取出产品
try {
Thread.sleep((int)(Math.random()*1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable {//生产者
Storage s = null;
public Producer(Storage s){
this.s = s;
}
public void run() {
for(int i=0; i<20; i++){
Product p = new Product(i);
s.push(p);
//放入产品
try {
Thread.sleep((int)(Math.random()*1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Product {
int id;
public Product(int id){
this.id = id;
}
public String toString(){//重写toString方法
return "产品:"+this.id;
}
}
class Storage {
int index = 0;
Product[] products = new Product[5];
public synchronized void push(Product p){//放入
while(index==this.products.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.products[index] = p;
System.out.println("生产者放入"+index+"位置:" + p);
index++;
this.notifyAll();
}
public synchronized Product pop(){//取出
while(this.index==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
index--;
this.notifyAll();
System.out.println("消费者从"+ index+ "位置取出:" + this.products[index]);
return this.products[index];
}
}
相关文章推荐
- java多线程之生产者与消费者问题的简单模拟
- java多线程模拟生产者消费者问题,公司面试常常问的题。。。
- java线程模拟生产者消费者问题
- Java 多线程 生产者与消费者问题测试代码
- java多线程(3)生产者消费者问题(二)
- java利用lock和unlock实现消费者与生产者问题(多线程)
- 生产者消费者问题--JAVA模拟
- java多线程之生产者消费者经典问题
- java多线程之生产者消费者经典问题
- 继续java多线程——生产者消费者类似问题
- java多线程-生产者消费者经典问题 基于BlockingQueue
- 由生产者/消费者问题看JAVA多线程
- java多线程总结六:经典生产者消费者问题实现
- java模拟生产者与消费者问题(线程同步)
- Java多线程之生产者/消费者问题
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
- java多线程之生产者消费者经典问题
- java多线程之生产者消费者经典问题
- java多线程总结六:经典生产者消费者问题实现
- 由生产者/消费者问题看JAVA多线程