马士兵生产者消费者

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(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;
		System.out.println("set: " + arrWT[index]);
		index++;
	}

	public synchronized WoTou pop() {
		while (index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notifyAll();
		index--;
		System.out.println("get :" + arrWT[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);
			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();
			try {
				Thread.sleep((int) (Math.random() * 1000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

 

下面是自己的demo:

 

import org.apache.log4j.Logger;


public class Consumer implements Runnable{
	private Container container;
	Logger log = Logger.getLogger(Consumer.class);
	
	public Consumer(Container container){
		this.container = container;
	}
	@Override
	public void run() {
		for(int i=0;i<10;i++){
			try {
				Thread.sleep((int) (Math.random() * 1000));
				container.get();
				//log.info("消费了:"+container.get());
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

 

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class Container {
	private List<Food> container;
	Logger log = Logger.getLogger(Container.class);

	public Container(List<Food> container){
		this.container = container;
	}
	
	public synchronized Food get() throws Exception{
		if(container == null){
			container = new ArrayList<Food>();
		}
		while(container.isEmpty()){
			this.wait();
		}
		Food food = container.remove(container.size()-1);
		log.info("get food:"+food+",container:"+container);
		this.notifyAll();
		return food;
	}
	
	public synchronized void set(Food food) throws Exception{
		if(container == null){
			container = new ArrayList<Food>();
		}
		while(container.size() >= 10){
			this.wait();
		}
		log.info("set food: "+food+",container:"+container);
		container.add(food);
		this.notifyAll();
	}
}

 

public class Food {

	private int id;

	public Food(int id){
		this.id = id;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "Food [id=" + id + "]";
	}
	
}

 

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ProducerConsumerTest {

	public static void main(String[] args) {
		List<Food> list = new ArrayList<Food>();
		Container container = new Container(list);
		Producer p = new Producer(container);
		Consumer c = new Consumer(container);
		//new Thread(p).start();
		//new Thread(p2).start();
		//new Thread(c).start();
		ExecutorService pool = Executors.newFixedThreadPool(2);
		pool.execute(p);
		pool.execute(c);
	}
	
}

 

你可能感兴趣的:(生产者消费者,马士兵)