日期:2014-05-20  浏览次数:20694 次

200分求生产者和消费者的多线程的代码
RT

------解决方案--------------------

public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
}
}

class WoTou {
int id; 
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou : " + id;
}
}

class SyncStack {
int index = 0;
WoTou[] arrWT = new WoTou[6];

public synchronized void push(WoTou wt) {
while(index == arrWT.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}

public synchronized WoTou pop() {
while(index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}

class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<20; i++) {
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
------解决方案--------------------
网上帮你找得

Java code
class Producter extends Thread
{
Queue q;
Producter (Queue q)
{
this.q=q;
}
public void run()
{
for(int i=0;i<10;i++)
{
q.put(i);
System.out.println("producter :"+i);
}
}
}
class Consumer extends Thread
{
Queue q;
Consumer(Queue q)
{
this.q=q;
}
public void run()
{
while(true)
{
System.out.println("Consumer:"+q.get());
}
}
}
class Queue //key
{
int value;
boolean bFull=false;
public synchronized void put(int i)
{
if(!bFull)
{
value=i;
bFull=true;
notify();//必须用在synchronized
}
try {
wait();//必须捕获异常 // 等待消费者取走数据
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized int get()
{
if(!bFull)
try
{
wait(); // 等待生产者写入数据
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bFull=false; // 通知生产者数据已经被取走,可以再次写入数据
notify();
return value;
}
}
public class test //测试类
{
public static void main(String[] args)
{
Queue q=new Queue();
Producter p=new Producter(q);
Consumer c=new Consumer(q);
p.start();
c.start();
}
}
问题补充:System.out.println("Consumer:"+q.get());
}
}
}
class Queue //key
{
int value;
boolean bFull=false;
public synchronized void put(int i)
{
if(!bFull)
{
value=i;
bFull=true;
notify();//必须用在synchronized
}
try {
wait();//必须捕获异常 // 等待消费者取走数据
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized int get()
{
if(!bFull)
try
{
wait(); // 等待生产者写入数据
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bFull=false; // 通知生产者数据已经被取走,可以再次写入数据
notify();
return value;
}
}
public class test //测试类
{
public static void main(String[] args)
{
Queue q=new Queue();
Producter p=new Producter(q);
Consumer c=new Consumer(q);
p.start();
c.start();
}
}