`
ronaldoLY
  • 浏览: 42147 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

生产/消费模型的java实现

阅读更多

         生产者消费者模型,其实就是一个(生产者)负责产生数据,另一个(消费者)负责使用数据,这里的数据就是生产者和消费者共用的资源,为了让生产者和消费者能有序地使用公共资源,需要引入锁(synchronized)的概念----在一段代码中,将那一段需要很多线程共同使用的代码(相当于共享资源)用synchronized关键字定义,相当于给这一段区域进行了加锁,当有线程来操作它时,就会对其加锁,其他的线程在此时若准备操作它只能排队等待,等第一个线程操作完成,锁解除之后,才能操作。

      下面实现的生产消费模型主要是:

       1.仓库中无产品时,生产者开始生产一件放入仓库,通知消费者来取;

       2.消费者从仓库中取出产品后,仓库没有库存,通知生产者去继续生产。

       3.生产者和消费者是两个互不干扰的线程,但是又有一定的联系,联系就是通过仓库这个被锁定的区域实现的。

      4.wait()和notify()方法就是生产者和消费者线程之间的联系,当一方在使用公共资源时,另一方的状态为wait,当这一方使用公共资源完毕后,会notify(通知)等待的一方。

 

生产者代码:

package Producer_customer0123;

import java.util.List;

public class ProducerThread extends Thread {

	private List<Things> thingslist;//产品队列
	int count=0;
	
	public ProducerThread(List<Things> thingslist){
		this.thingslist=thingslist;
	}
	public void run(){
		while(true){
			//休眠2秒
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e1) {
				
				e1.printStackTrace();
			}
			
			synchronized (thingslist) {
				while(thingslist.size()>0){//如果有产品,则等待
					try {
						thingslist.wait();
						
						System.out.println("生产者在等待-------生产者线程");
						
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				while(thingslist.size()==0){//如果没有产品,则生产产品,并且通知消费者
					Things newthing=new Things();
					count++;
					newthing.id=count;
					newthing.name="第"+count+"个产品";
					thingslist.add(newthing);//加入到队列中
					
					thingslist.notify();//通知消费者
					System.out.println("生产者生产了"+count+"号产品-------生产者线程");
				}
			}
		}
	}
}

 

消费者代码:

package Producer_customer0123;

import java.util.List;

public class CustomerThread extends Thread{

	private List<Things> thingslist;

	public CustomerThread(List<Things> thingslist) {
		this.thingslist=thingslist;
	}

	public void run(){
		
		while(true){
			
			synchronized (thingslist) {
				while(thingslist.size()>0){//如果队列中有产品,消费者就取出来
					for(int i=0;i<thingslist.size();i++){
						
						thingslist.remove(i);
						thingslist.notify();
						System.out.println("消费者取出了队列中第"+i+"号产品----消费者线程");
					}
				}
				while(thingslist.size()==0){
					try {
						System.out.println("队列中已没有产品了----消费者线程");
						thingslist.wait();
						
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

}

 

主函数:

package Producer_customer0123;

import java.util.LinkedList;
import java.util.List;

public class Test {

	
	public static void main(String args[]){
		//创建队列
		List<Things> thingslist=new LinkedList<Things>() ;
		
		//启动线程
		ProducerThread producer=new ProducerThread(thingslist);
		CustomerThread customer=new CustomerThread(thingslist);
		producer.start();
		customer.start();
	}

}

 

运行结果如下:

 

 

 

      结论:通过打印会发现生产者线程和消费者线程会按照某一个顺序依次对thingslist进行操作,对比两段线程的代码,会发现,一个很大的不同就是wait()和notify()的时机,生产者和消费者总是相反的,这也正好解释了在同一时刻,thingslist只允许一个线程对他进行操作。

     以上就是我对生产消费模型的一些认识。
 

 

  • 大小: 37.3 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics