您的位置:首页 > 编程语言 > Java开发

java实现 生产者和消费者问题 多线程同步示例

2008-03-16 16:05 786 查看
package com.producerconsumer;

/**
* <p>
* Title:产品
* </p>
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date 2008-3-16 下午02:50:12
*/
public class Produce {

/**
* 产品名称
*/
private String name;

public Produce(String name) {
super();
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}



package com.producerconsumer;

/**
* <p>
* Title: 产品仓库
* </p>
*
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date 2008-3-16 下午02:54:50
*/
public class Storage {
private final static int SIZE = 100;// 队列大小

private Produce[] produces = new Produce[SIZE];// 模拟队列

private int front = 0;// 头下标

private int rear = 0;// 尾下标

public Produce getProduce() {
synchronized (this) {
while (front == rear) {
try {
System.out.println("无可消费的产品!");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Produce produce = produces[front];
front = (SIZE + 1 + front) % SIZE;// 从1开始,最大size-1
System.out.println("消费=>" + produce.getName());
this.notifyAll();
return produce;
}
}

public void putProduce(Produce produce) {
synchronized (this) {
while ((rear + 1) % SIZE == front) {
try {
System.out.println("仓库已满!");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
produces[rear] = produce;
rear = (rear + 1) % SIZE;// 从0开始,最大size-1
System.out.println("生产=>" + produce.getName());
this.notifyAll();
}

}
}

package com.producerconsumer;

/**
* <p>
* Title: 生成者
* </p>
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date 2008-3-16 下午02:53:44
*/
public class Producer extends Thread {
private Storage storage;

public Producer(Storage storage) {
super();
this.storage = storage;
}

public void run() {
while (true) {
Produce produce = new Produce("syj");
storage.putProduce(produce);
// System.out.println("生产:" + produce.getName());
try {
sleep((int) (Math.random() * 10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

package com.producerconsumer;

/**
* <p>
* Title:消费者
* </p>
*
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date 2008-3-16 下午03:06:56
*/
public class Consumer extends Thread {
private Storage storage;

public Consumer(Storage storage) {
super();
this.storage = storage;
}

public void run() {
while (true) {
Produce produce = storage.getProduce();
// System.out.println("消费:" + produce.getName());
try {
sleep((int) (Math.random() * 10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

package com.producerconsumer;

/**
* <p>
* Title: 测试生产者消费者
* </p>
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date 2008-3-16 下午03:11:05
*/
public class Test {
public static void main(String[] args) {
Storage storage = new Storage();

Producer producer1 = new Producer(storage);
Producer producer2 = new Producer(storage);
Producer producer3 = new Producer(storage);
Producer producer4 = new Producer(storage);

Consumer consumer1 = new Consumer(storage);
Consumer consumer2 = new Consumer(storage);
Consumer consumer3 = new Consumer(storage);
Consumer consumer4 = new Consumer(storage);
Consumer consumer5 = new Consumer(storage);
Consumer consumer6 = new Consumer(storage);

producer1.start();
producer2.start();
consumer1.start();
consumer2.start();
consumer3.start();
producer3.start();
producer4.start();
consumer4.start();
consumer5.start();
consumer6.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: