生产者消费者实例
2011-01-13 01:41
148 查看
package com.thread.test;
public class ProductorConsumer {
public static void main(String[] args) {
WoTouPool pool = new WoTouPool();
Productor p1 = new Productor("p1", pool);
Productor p2 = new Productor("p2", pool);
Productor p3 = new Productor("p3", pool);
Consumer c1 = new Consumer("c1", pool);
Consumer c2 = new Consumer("c2", pool);
Consumer c3 = new Consumer("c3", pool);
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
new Thread(c1).start();
new Thread(c2).start();
new Thread(c3).start();
}
}
class WoTouPool {
private int index = 0;
private WoTou[] woTous = new WoTou[10];
private Object obj = new Object();
public synchronized void pushWoTou(WoTou woTou) {
while (index == woTous.length) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj.notify();
woTous[index] = woTou;
index++;
System.out
.println(" 当前WoTou个数:"
+ index);
}
public synchronized WoTou popWoTou() {
while (index == 0) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj.notify();
index--;
System.out
.println(" 当前WoTou个数:"
+ index);
return woTous[index];
}
}
class WoTou {
private String name;
public WoTou(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Productor implements Runnable {
private String name;
private WoTouPool woTouPool;
public Productor(String name, WoTouPool woTouPool) {
this.name = name;
this.woTouPool = woTouPool;
}
public void run() {
for (int i = 0; i < 20; i++) {
WoTou woTou = new WoTou(name + " 生产的第 " + (i + 1) + " 个产品");
woTouPool.pushWoTou(woTou);
System.out.println(woTou.getName());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private String name;
private WoTouPool woTouPool;
public Consumer(String name, WoTouPool woTouPool) {
this.name = name;
this.woTouPool = woTouPool;
}
public void run() {
for (int i = 0; i < 20; i++) {
WoTou woTou = woTouPool.popWoTou();
System.out.println(name + " 第 " + (i + 1) + " 次消费的产品,该产品的信息:"
+ woTou.getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class TestStatic {
private static Object obj = new Object();
public synchronized static void f() {
System.out.println("---f-----");
}
public static void g() {
synchronized (obj) {
System.out.println("---g-----");
}
}
}
public class ProductorConsumer {
public static void main(String[] args) {
WoTouPool pool = new WoTouPool();
Productor p1 = new Productor("p1", pool);
Productor p2 = new Productor("p2", pool);
Productor p3 = new Productor("p3", pool);
Consumer c1 = new Consumer("c1", pool);
Consumer c2 = new Consumer("c2", pool);
Consumer c3 = new Consumer("c3", pool);
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
new Thread(c1).start();
new Thread(c2).start();
new Thread(c3).start();
}
}
class WoTouPool {
private int index = 0;
private WoTou[] woTous = new WoTou[10];
private Object obj = new Object();
public synchronized void pushWoTou(WoTou woTou) {
while (index == woTous.length) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj.notify();
woTous[index] = woTou;
index++;
System.out
.println(" 当前WoTou个数:"
+ index);
}
public synchronized WoTou popWoTou() {
while (index == 0) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj.notify();
index--;
System.out
.println(" 当前WoTou个数:"
+ index);
return woTous[index];
}
}
class WoTou {
private String name;
public WoTou(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Productor implements Runnable {
private String name;
private WoTouPool woTouPool;
public Productor(String name, WoTouPool woTouPool) {
this.name = name;
this.woTouPool = woTouPool;
}
public void run() {
for (int i = 0; i < 20; i++) {
WoTou woTou = new WoTou(name + " 生产的第 " + (i + 1) + " 个产品");
woTouPool.pushWoTou(woTou);
System.out.println(woTou.getName());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private String name;
private WoTouPool woTouPool;
public Consumer(String name, WoTouPool woTouPool) {
this.name = name;
this.woTouPool = woTouPool;
}
public void run() {
for (int i = 0; i < 20; i++) {
WoTou woTou = woTouPool.popWoTou();
System.out.println(name + " 第 " + (i + 1) + " 次消费的产品,该产品的信息:"
+ woTou.getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class TestStatic {
private static Object obj = new Object();
public synchronized static void f() {
System.out.println("---f-----");
}
public static void g() {
synchronized (obj) {
System.out.println("---g-----");
}
}
}
相关文章推荐
- JAVA多线程模拟生产者与消费者_实例
- 生产者消费者实例
- Kafka消费者生产者实例
- BlockingQueue—生产者消费者实例
- Java 实例 - 生产者/消费者问题
- Java中线程同步—————生产者与消费者实例
- java中多线程通信实例:生产者消费者模式
- Python实现的生产者、消费者问题完整实例
- php rabbitmq操作类及生产者和消费者实例代码
- 黑马程序员_多线程:生产者消费者练习实例及问题
- 线程中消费者生产者的实例代码(synchronized关键字)
- 《Lua程序设计》9.2 管道(pipe)与过滤器(filter) 包含使用协同函数实现“生产者——消费者”问题的实例代码
- 生产者消费者实例!!!
- 【消息队列】ActiveMQ的简单实例 - 生产者消费者模式
- Linux C++ 面向对象多线程编程实例之“生产者-消费者”
- php rabbitmq操作类及生产者和消费者实例代码
- java之多线程实例 生产者与消费者
- java生产者消费者实例
- java中用一个MyNumber类充当“生产者—消费者”模式的缓冲池实例
- 基于wait和notify的生产者消费者实例