生产者消费者问题(java)

  哦,今天用java做了一下生产者、消费者问题,感觉代码有点脑残,不过希望分享出来起到抛砖引玉的目的,感觉这个程序不太令人满意的地方是:

1)生产者必须全部生产完毕(10个)后,消费者才可以消费。

2)消费者必须全部消费完毕(10个)后,生产者才可以生产。

程序段一:

class Q
{
    String name;
    int num=0;
    int size=10;
}

class Producer implements Runnable
{
    Q q;
    Producer(Q q)
    {
    	this.q = q;
    	this.q.name="producer";
    }
    public void run()
    {
        while(true)
        {
            synchronized(q)
            {
            	if(q.num<q.size)
            	{
            		q.num++;
            		System.out.println("producer"+q.num);
            		q.notify();
                }
            	else
            	{
            		try {
            			System.out.println("producer stop!");
						q.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
            	}
            }
        }
    }
}

class Consumer implements Runnable
{
    Q q;
    Consumer(Q q)
    {
        this.q = q;
        this.q.name="consumer";
    }
    public void run()
    {
        while(true)
        {
            synchronized(q)
            {
                if(q.num>0)
                {
                	System.out.println("consumer"+q.num);
                	q.num--;             
                	q.notifyAll();
                }
                else
                {
                	try {
                		System.out.println("consumer stop!");
						q.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
            }
        }
    }
}

public class project
{
    public static void main(String[] args)
    {
        Q q = new Q();
        new Thread(new Producer(q)).start();
        new Thread(new Consumer(q)).start();
    }
}

程序段二:

 
class mythread implements Runnable{
	public int size;                           //定义资源池的大小
	public int n;                              //定义资源池现有资源数目
	public mythread(){
		size=100;
		n=0;
	}
	public synchronized void ChangeSource()
	{
		if(Thread.currentThread().getName().equals("生产者"))   //判断是否是生产者线程
		{
			n++;
			System.out.println("生产者进程已经生产,当前资源"+n);
			notifyAll();
		}
		else                                                   //否则是消费者线程
		{
			n--;
			System.out.println("消费者进程已经消费,当前资源"+n);
			notifyAll();
		}
	}
	public void run() {
		while(true)
		{
			if(Thread.currentThread().getName().equals("生产者")){               //当前进程是生产者进程
				if(n<size)                                                       //如果资源池中资源未满
				ChangeSource();
				else                                                             //否则资源池中的资源已经满
				{
				/*	try {
						this.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}*/
					try {
						System.out.println("生产者进程阻塞!");
				        Thread.currentThread().sleep(1000);           //阻塞当前进程
					} 
					catch (InterruptedException e){
						e.printStackTrace();
					}
				}
			}
			else                                                                 //当前进程是消费者进程
			{
				if(n>0)                                                          //如果资源池中还有资源
				ChangeSource();
				else                                                             //否则资源池中的资源用尽
				{
					System.out.println("消费者进程阻塞!");
					/*try {
						this.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}*/
					try{
						System.out.println("消费者进程阻塞!");
						Thread.currentThread().sleep(1000);//Thread.currentThread().wait();            //阻塞当前进程
					}
					catch(InterruptedException e){
						e.printStackTrace();
					}
				}
			}
			//k++;
		}
	}
}
public class project {
    public static void main(String[] args) {
    	mythread base=new mythread();
    	Thread producer,consumer;
    	producer=new Thread(base);
    	consumer=new Thread(base);
    	producer.setName("生产者");
    	consumer.setName("消费者");
    	producer.start();
    	consumer.start();
    }

}

最后求大神不理赐教,,,

态度决定高度,细节决定成败,
原文地址:https://www.cnblogs.com/lxk2010012997/p/2829565.html