一个生产者 多个消费者

将传统生产者消费者模式里的notify  换成了  notifyAll

 

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

public class ProducerModular {

	public static void main(String[] args) throws InterruptedException {
		MyShare share = new MyShare();

		Thread provider = new MyProvider(share);
		Thread bailing = new MyBailing(share, "baiLing one ");

		Thread bailing2 = new MyBailing(share, "baiLing two ");

		provider.setDaemon(true);
		bailing.setDaemon(true);

		bailing2.setDaemon(true);

		System.out.println("0.01秒生产消费者模式");
		bailing.start();
		bailing2.start();
		provider.start();

		Thread.sleep((long) (0.01 * 1000));

		provider.interrupt();// 中断结束线程
		bailing.interrupt(); // 中断结束线程
		bailing2.interrupt();// 中断结束线程

		provider.join();
		bailing.join();
		bailing2.join();
		System.out.println("--------------| 结束");
	}
}

/**
 * 生产者
 * 
 * @author chengkai
 * 
 */
class MyProvider extends Thread {

	private MyShare myshare;

	public MyProvider(MyShare myshare) {
		super();
		this.myshare = myshare;
	}

	// private Thread cThread;
	@Override
	public void run() {
		// this.cThread=Thread.currentThread();
		try {
			while (!Thread.currentThread().isInterrupted()) {

				String str = "" + (char) this.random(1, 255);
				myshare.charSetting(str);
				// System.out.println("生产了                                    :    "
				// + str);

			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			return;
		}
	}

	public int random(int start, int end) {
		double result = Math.random() * (end - start + 1) + start;
		return (int) result;
	}

	// public void toStop(){
	//		
	// }

}

/**
 * 消费者(白领)
 * 
 * @author chengkai
 * 
 */
class MyBailing extends Thread {

	private MyShare myshare;

	public MyBailing(MyShare myshare, String name) {
		super(name);
		this.myshare = myshare;
	}

	@Override
	public void run() {
		try {
			while (!Thread.currentThread().isInterrupted()) {
				String str = myshare.getCharVar();
				// System.out.println(this.getName()+"  消费了:    " + str);
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			return;
		}
	}

}

/**
 * 通道 //适用于1个生产者 ,多个消费者  
 * 
 * @author chengkai
 * 
 */
class MyShare {

	List<String> shareList = Collections.synchronizedList(new LinkedList());

	public synchronized void charSetting(String product)
			throws InterruptedException {

		while (shareList.size() != 0) {
			this.wait();
		}

		this.push(product);
		System.out.println(Thread.currentThread().getName()
				+ " 生产了             ---|  " + product);
		this.notifyAll();

	}

	public synchronized String getCharVar() throws InterruptedException {

		while (shareList.size() == 0) {
			this.wait();
		}

		String str = this.popLast();
		System.out.println(Thread.currentThread().getName() + " 消费了   ---|  "
				+ str);
		this.notifyAll();
		return str;

	}

	/*********************
	 * 设置和输出队列
	 * 
	 * @param obj
	 */

	private void push(String obj) {
		synchronized (shareList) {
			shareList.add(0, obj);
		}
	}

	private String popLast() {
		synchronized (shareList) {
			if (shareList.size() == 0) {
				return null;
			}
			return shareList.remove(this.shareList.size() - 1);
		}
	}

}

输出结果:
0.01秒生产消费者模式
Thread-0 生产了        ---|  ú
baiLing two  消费了   ---|  ú
Thread-0 生产了        ---|  Ò
baiLing one  消费了   ---|  Ò
Thread-0 生产了        ---|  9
baiLing two  消费了   ---|  9
Thread-0 生产了        ---|  
baiLing one  消费了   ---|  
Thread-0 生产了        ---|  ,
baiLing two  消费了   ---|  ,
Thread-0 生产了        ---|  i
baiLing one  消费了   ---|  i
Thread-0 生产了        ---|  
baiLing two  消费了   ---|  
Thread-0 生产了        ---|  l
baiLing one  消费了   ---|  l
Thread-0 生产了        ---|  »
baiLing two  消费了   ---|  »
Thread-0 生产了        ---|  «
baiLing one  消费了   ---|  «
Thread-0 生产了        ---|  ß
baiLing two  消费了   ---|  ß
Thread-0 生产了        ---|  |
baiLing one  消费了   ---|  |


...... 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值