简单消费者生产者问题
2011-08-02 11:21
330 查看
import java.util.*;
public class HelloWorld {
public static void main(String[] args){
dataStore m = new dataStore();
Producer p = new Producer("P",m);
Consumer c1= new Consumer("A",m);
Consumer c2= new Consumer("B",m);
Consumer c3= new Consumer("C",m);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
Thread t3 = new Thread(c3);
Thread t4 = new Thread(p);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Producer implements Runnable{
String name ;
private dataStore myDataStore;
public Producer(String s, dataStore m){
name = s ;
myDataStore = m;
}
public void run(){
for(int i = 0 ;i < 15 ;i++ ){
int number = (int)(Math.random()*100);
myDataStore.putData(number,name);
}
try{
Thread.sleep((int)(200*Math.random()));
}
catch(InterruptedException e){}
}
}
class Consumer implements Runnable{
private String name ;
private dataStore myDataStore;
public Consumer(String s,dataStore m){
name = s ;
myDataStore = m;
}
public void run(){
int number;
for(int i = 0 ;i < 5 ;i++ ){
number = myDataStore.getData(name);
}
}
}
class dataStore{
private int[] data = new int[4];
private int haveData = 0;
public synchronized int getData(String name){
while (haveData <= 0){
try{
wait();
}
catch (InterruptedException e ){}
}
haveData--;
notify();
System.out.println(" Consumer " + name + " consume the data " + data[haveData]);
return data[haveData];
}
public synchronized void putData(int v,String name){
while(haveData >=3 ){
try{
wait();
}
catch(InterruptedException e){}
}
data[haveData] = v;
haveData++;
System.out.println(" Pro " + name + " produce the numble " + v );
notify();
}
}
public class HelloWorld {
public static void main(String[] args){
dataStore m = new dataStore();
Producer p = new Producer("P",m);
Consumer c1= new Consumer("A",m);
Consumer c2= new Consumer("B",m);
Consumer c3= new Consumer("C",m);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
Thread t3 = new Thread(c3);
Thread t4 = new Thread(p);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Producer implements Runnable{
String name ;
private dataStore myDataStore;
public Producer(String s, dataStore m){
name = s ;
myDataStore = m;
}
public void run(){
for(int i = 0 ;i < 15 ;i++ ){
int number = (int)(Math.random()*100);
myDataStore.putData(number,name);
}
try{
Thread.sleep((int)(200*Math.random()));
}
catch(InterruptedException e){}
}
}
class Consumer implements Runnable{
private String name ;
private dataStore myDataStore;
public Consumer(String s,dataStore m){
name = s ;
myDataStore = m;
}
public void run(){
int number;
for(int i = 0 ;i < 5 ;i++ ){
number = myDataStore.getData(name);
}
}
}
class dataStore{
private int[] data = new int[4];
private int haveData = 0;
public synchronized int getData(String name){
while (haveData <= 0){
try{
wait();
}
catch (InterruptedException e ){}
}
haveData--;
notify();
System.out.println(" Consumer " + name + " consume the data " + data[haveData]);
return data[haveData];
}
public synchronized void putData(int v,String name){
while(haveData >=3 ){
try{
wait();
}
catch(InterruptedException e){}
}
data[haveData] = v;
haveData++;
System.out.println(" Pro " + name + " produce the numble " + v );
notify();
}
}
相关文章推荐
- 生产者和消费者问题(java简单实现)
- java多线程之生产者与消费者问题的简单模拟
- 生产者与消费者问题是典型的同步问题。这里简单介绍两种不同的实现方法。
- Java- 简单了解线程 生产者与消费者问题(三)
- 非常简单的方法实现生产者和消费者问题
- 简单实现生产者消费者问题
- Java- 简单了解线程 生产者与消费者问题(三)
- 最简单的生产者/消费者问题
- Java实现 简单的多线程“生产者-消费者”问题
- java简单模拟生产者消费者问题
- Java简单实现生产者消费者问题(源自2015年阿里巴巴实习生校园招聘)
- 操作系统 简单的单生产者单消费者问题
- 生产者消费者问题简单解决之ArrayBlockingQueue的详解与使用
- Java中简单的生产者和消费者问题
- java简单小结(附一个java实现的生产者消费者问题的程序)
- java多线程之生产者与消费者问题的简单模拟
- 使用ReentrantLock模拟简单生产者和消费者问题
- jdk线程的生产者消费者问题
- 经典问题生产者与消费者java并发线程模拟实现
- Java实现生产者消费者问题与读者写者问题