随意写的 消费者-生产者 示例

使用了自己写的log4j拓展显示线程ID的jar包。

 

运行效果:

写道
[2014-03-27 23:36:20.818][9][com.zwh.log4jtest.Storage][INFO]: producter int <<534, and list is [].+.
[2014-03-27 23:36:21.832][9][com.zwh.log4jtest.Storage][INFO]: producter int <<92, and list is [534].+.
[2014-03-27 23:36:22.847][9][com.zwh.log4jtest.Storage][INFO]: producter int <<30, and list is [534, 92].+.
[2014-03-27 23:36:23.862][8][com.zwh.log4jtest.Storage][INFO]: consumer int is >>534, and list is [92, 30]...
[2014-03-27 23:36:24.877][8][com.zwh.log4jtest.Storage][INFO]: consumer int is >>92, and list is [30]...
[2014-03-27 23:36:25.892][10][com.zwh.log4jtest.Storage][INFO]: consumer int is >>30, and list is []...
[2014-03-27 23:36:26.907][27][com.zwh.log4jtest.Storage][INFO]: producter int <<30, and list is [].+.
[2014-03-27 23:36:27.922][25][com.zwh.log4jtest.Storage][INFO]: producter int <<783, and list is [30].+.
[2014-03-27 23:36:28.937][24][com.zwh.log4jtest.Storage][INFO]: consumer int is >>30, and list is [783]...
[2014-03-27 23:36:29.952][24][com.zwh.log4jtest.Storage][INFO]: consumer int is >>783, and list is []...
[2014-03-27 23:36:30.967][23][com.zwh.log4jtest.Storage][INFO]: producter int <<952, and list is [].+.
[2014-03-27 23:36:31.982][21][com.zwh.log4jtest.Storage][INFO]: producter int <<901, and list is [952].+.
[2014-03-27 23:36:32.997][21][com.zwh.log4jtest.Storage][INFO]: producter int <<93, and list is [952, 901].+.
[2014-03-27 23:36:34.012][20][com.zwh.log4jtest.Storage][INFO]: consumer int is >>952, and list is [901, 93]...
[2014-03-27 23:36:35.027][20][com.zwh.log4jtest.Storage][INFO]: consumer int is >>901, and list is [93]...
[2014-03-27 23:36:36.042][19][com.zwh.log4jtest.Storage][INFO]: producter int <<884, and list is [93].+.
[2014-03-27 23:36:37.057][17][com.zwh.log4jtest.Storage][INFO]: producter int <<418, and list is [93, 884].+.
[2014-03-27 23:36:38.072][17][com.zwh.log4jtest.Storage][INFO]: producter int <<323, and list is [93, 884, 418].+.
[2014-03-27 23:36:39.087][17][com.zwh.log4jtest.Storage][INFO]: producter int <<45, and list is [93, 884, 418, 323].+.
[2014-03-27 23:36:40.102][16][com.zwh.log4jtest.Storage][INFO]: consumer int is >>93, and list is [884, 418, 323, 45]...

 

 

代码:

package com.zwh.log4jtest;

import java.util.LinkedList;
import java.util.Random;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class Storage {
	
	Logger logger = LogManager.getLogger(Storage.class);
	
	LinkedList<Integer> storage = new LinkedList<Integer>();
	Random random = new Random();
	
	public synchronized void consume()
	{
		if(storage.size() <= 0)
		{
			try {
				wait();
			} catch (InterruptedException e) {
				logger.error(e.getMessage());
			}
		}
		else
		{
			Integer i = storage.remove();
			logger.info("consumer int is >>" + i + ", and list is " + storage + "...");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			notify();
		}
		
	}
	
	public synchronized void product()
	{
		if(storage.size() >= 10)
		{
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		else
		{
			Integer i = random.nextInt(1000);
			logger.info("producter int <<" + i + ", and list is " + storage + ".+.");
			storage.add(i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			notify();
		}
	}
	
	
	
	public static void main(String[] args) {

		Storage storage = new Storage();
		
		for(int i = 0;i < 10;i ++)
		{
		Consumer consumer = new Consumer(storage);
		Producter producter = new Producter(storage);
		consumer.start();
		producter.start();
		}
		
		
		
	}
}
class Consumer extends Thread{
	
	private Storage storage;
	
	public Consumer(Storage storage)
	{
		this.storage = storage;
	}
	
	@Override
	public void run() {
		while(true)
		{
			storage.consume();
		}
	}
}
class Producter extends Thread{
	
	private Storage storage;
	
	public Producter(Storage storage)
	{
		this.storage = storage;
	}
	
	@Override
	public void run() {
		
		while(true)
		{
			storage.product();
		}
	}
}

 

你可能感兴趣的:(生产者)