`

java 多线程 producer customer

阅读更多
package com.thread2;

//java多线程模拟生产者消费者问题
//ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品
//Storage仓库
public class ProducerConsumer {

	public static void main(String[] args) {
		Storage s = new Storage();
		Producer p = new Producer(s);
		Consumer c = new Consumer(s);
		Thread tp = new Thread(p);
		Thread tc = new Thread(c);
		tp.start();
		tc.start();

	}
}

class Consumer implements Runnable {// 消费者
	Storage s = null;

	public Consumer(Storage s) {
		this.s = s;
	}

	public void run() {
		for (int i = 0; i < 20; i++) {
			Product p = s.pop();// 取出产品
			try {
				Thread.sleep((int) (Math.random() * 1500));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

}

class Producer implements Runnable {// 生产者
	Storage s = null;

	public Producer(Storage s) {
		this.s = s;
	}

	public void run() {
		for (int i = 0; i < 20; i++) {
			Product p = new Product(i);
			s.push(p); // 放入产品
			// System.out.println("生产者放入:" + p);
			try {
				Thread.sleep((int) (Math.random() * 1500));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}
}

class Product {
	int id;

	public Product(int id) {
		this.id = id;
	}

	public String toString() {// 重写toString方法
		return "产品:" + this.id;
	}
}

class Storage {
	int index = 0;
	Product[] products = new Product[5];

	public synchronized void push(Product p) {// 放入
		while (index == this.products.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.products[index] = p;
		System.out.println("生产者放入" + index + "位置:" + p);
		index++;
		this.notifyAll();
	}

	public synchronized Product pop() {// 取出
		while (this.index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		index--;
		this.notifyAll();
		System.out.println("消费者从" + index + "位置取出:" + this.products[index]);
		return this.products[index];
	}
}
 wait导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
wait(),notify(),notifyAll()不属于Thread类,而是属于Object基础类,也就是说每个对像都有wait(),notify(),notifyAll()的功能.因为都个对像都有锁,锁是每个对像的基础,当然操作锁的方法也是最基础了. 




package com.test.thread;

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

public class CustomerProducer {
	public static void main(String args[]){
		Storage s = new Storage();
		Producer p = new Producer(s);
		Customer c = new Customer(s);
		Thread tp = new Thread(p);
		Thread cp = new Thread(c);
		tp.start();
		cp.start();
	}
}
class Storage{
	List<String> storage = new ArrayList<String>();
	int maxLength = 10;
	int index = 0;
	public synchronized void push(String goods){
		while(this.index == maxLength){
			try{
				System.out.println("push wait");
				this.wait();
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		storage.add(goods);
		System.out.println("producer index:"+(index+1));
		index++;
		this.notifyAll();
	}
	
	public synchronized String pop(){
		while(this.index == 0){
			try{
				System.out.println("pop wait");
				this.wait();
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		String tmp = storage.remove(index-1);
		System.out.println("customer index:"+index);
		index--;
		this.notifyAll();
		return tmp;
	}
}
class Producer implements Runnable{
	Storage s = null;
	public Producer(Storage s){
		this.s = s;
	}
	public void run(){
		for(int i = 0;i<10;i++){
			String goods = String.valueOf(i);
			s.push(goods);
			try{
				Thread.sleep((int)(Math.random()*1000));
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
}
class Customer implements Runnable{
	Storage s = null;
	public Customer(Storage s){
		this.s = s;
	}
	public void run(){
		for(int i = 0;i<10;i++){
//			String str = s.pop();
//			System.out.println(str);
			s.pop();
			try{
				Thread.sleep((int)(Math.random()*3000));
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics