java生产者与消费者问题_java生产者与消费者问题
為了回憶一下J2SE中的線程互斥與同步問題,所以今天就寫個生產者與消費者問題。這個程序大部分時間的結果都基本正確,但某些時候會造成死鎖。百思不得其解,將代碼貼上,方便以后有更深的體會時再進行修改。也方便各位同學借鑒與指正。
代碼如下:
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author passzh
* 2010.11.7 10:30pm
* the problem of producter and consumer
*/
public class ProductAndConsumer {
public static void main(String[] args)
{
MyFun my=new MyFun();
}
}
class MyFun
{
//設置signal
static Integer empty=4;
static Integer full=0;
static Integer matrix=1;
//被掛起的線程的隊列
ArrayList list_p=new ArrayList();
ArrayList list_c=new ArrayList();
public MyFun()
{
Producter[] producter=new Producter[4];
Consumer[] consumer=new Consumer[4];
for(int i=0;i<4;i++)
{
producter[i]=new Producter(i);
consumer[i]=new Consumer(i);
producter[i].start();
consumer[i].start();
}
}
//定義PV操作
public void p(MyThread mythread,int tag)
{
//tag==1是為生產者,為2時是消費者
if(tag==1)
{
synchronized(MyFun.empty)
{
MyFun.empty--;
//System.out.println("p操作empty="+MyFun.empty);
}
if(MyFun.empty<0)
{
try {
synchronized(mythread)
{
//String name=tag==1?"生產者":"消費者";
System.out.println("生產者"+mythread.getNo()+"被掛起");
list_p.add(mythread);
mythread.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(ProductAndConsumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
else if(tag==2)
{
synchronized(MyFun.full)
{
MyFun.full--;
//System.out.println("p操作full="+MyFun.full);
}
if(MyFun.full<0)
{
try {
synchronized(mythread)
{
//String name=tag==1?"生產者":"消費者";
System.out.println("消費者"+mythread.getNo()+"被掛起");
list_c.add(mythread);
mythread.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(ProductAndConsumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
//定義V操作
public void v(int tag)
{
if(tag==1)
{
synchronized(MyFun.full)
{
MyFun.full++;
//System.out.println("v操作full="+MyFun.full);
}
if(MyFun.full<=0)
{
//System.out.println();
if(list_c.size()>0)
{
MyThread thr=list_c.get(0);
synchronized(thr)
{
list_c.remove(0);
thr.notify();
System.out.println("消費者"+thr.getNo()+"被喚醒");
}
}
}
}
else if(tag==2)
{
synchronized(MyFun.empty)
{
MyFun.empty++;
//System.out.println("v操作empty="+MyFun.empty);
}
if(MyFun.empty<=0)
{
if(list_p.size()>0)
{
MyThread thr=list_p.get(0);
synchronized(thr)
{
list_p.remove(0);
thr.notify();
System.out.println("生產者"+thr.getNo()+"被喚醒");
}
}
}
}
}
class MyThread extends Thread
{
protected int no;
public int getNo()
{
return this.no;
}
}
class Producter extends MyThread
{
public Producter(int n)
{
this.no=n;
}
public void run()
{
synchronized(this)
{
p(this,1);
System.out.println("生產者"+this.getNo()+"生產一個產品");
v(1);
}
}
}
class Consumer extends MyThread
{
public Consumer(int n)
{
this.no=n;
}
public void run()
{
synchronized(this)
{
p(this,2);
System.out.println("消費"+this.getNo()+"消費一個產品");
v(2);
}
}
}
}
and result:
result 1:
生產者0生產一個產品
生產者1生產一個產品
消費0消費一個產品
消費1消費一個產品
生產者2生產一個產品
生產者3生產一個產品
消費2消費一個產品
消費3消費一個產品
result2:
生產者0生產一個產品消費1消費一個產品消費者0被掛起生產者1生產一個產品消費者0被喚醒消費0消費一個產品消費者2被掛起消費者3被掛起生產者2生產一個產品消費者2被喚醒消費2消費一個產品生產者3生產一個產品消費者3被喚醒消費3消費一個產品
總結
以上是生活随笔為你收集整理的java生产者与消费者问题_java生产者与消费者问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [HNOI2004]L语言
- 下一篇: Eclipse开发工具之崩溃和备份