多线程学习——消费者生产者(1)

package com.daicy.ProducerConsumer;

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

public class ProducerConsumer {
	public static void main(String[] args) {
		executorThreads();
		// baseThreads();
	}

	public static void executorThreads() {
		IDStack stack = new DLinkedBlockingQueue<Mantou>();
		ExecutorService exec = Executors.newFixedThreadPool(3);
		exec.execute(new Consumer(stack));
		exec.execute(new Consumer(stack));
		exec.execute(new Producer(stack));
		exec.execute(new Consumer(stack));
		exec.execute(new Producer(stack));
		// shutdownAndAwaitTermination(exec);
		try {
			TimeUnit.MILLISECONDS.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // Run for a while...
		exec.shutdownNow(); // Interrupt all tasks
		// new Thread(new Consumer(stack)).start();
		// new Thread(new Producer(stack)).start();
		//
		// new Thread(new Producer(stack)).start();
		// new Thread(new Consumer(stack)).start();
	}

	public static void shutdownAndAwaitTermination(ExecutorService pool) {
		pool.shutdown(); // Disable new tasks from being submitted
		try {
			// Wait a while for existing tasks to terminate
			if (!pool.awaitTermination(60, TimeUnit.MILLISECONDS)) {
				pool.shutdownNow(); // Cancel currently executing tasks
				// Wait a while for tasks to respond to being cancelled
				if (!pool.awaitTermination(60, TimeUnit.MILLISECONDS))
					System.err.println("Pool did not terminate");
			}
		} catch (InterruptedException ie) {
			// (Re-)Cancel if current thread also interrupted
			pool.shutdownNow();
			// Preserve interrupt status
			Thread.currentThread().interrupt();
		}
	}

	public static void baseThreads() {
		IDStack stack = new DLinkedBlockingQueue<Mantou>();
		List<Thread> list = new ArrayList();
		list.add(new Thread(new Consumer(stack)));
		list.add(new Thread(new Producer(stack)));
		list.add(new Thread(new Producer(stack)));
		list.add(new Thread(new Consumer(stack)));
		for (int i = 0; i < list.size(); i++) {
			list.get(i).start();
		}

		try {
			TimeUnit.MILLISECONDS.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // Run for a while...

		for (int i = 0; i < list.size(); i++) {
			list.get(i).interrupt();
		}

		// for (int i = 0; i < list.size(); i++) {
		// list.get(i).stop();
		// }
	}
}
package com.daicy.ProducerConsumer;

public class Consumer implements Runnable {
	private IDStack<Mantou> stack;

	private static int taskCount;

	private final int id = taskCount++;

	public Consumer(IDStack<Mantou> stack) {
		this.stack = stack;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		// while (true) {
		while (!Thread.interrupted()) {
			try {
				Mantou mantou = stack.pop();
				System.out.println("C" + this.id + " 消费了 mantou" + mantou);
				// Thread.sleep((long) (Math.random() * 1000));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("C" + this.id + " 消费了ttttt mantou");
				break;
			}
		}

	}
}

package com.daicy.ProducerConsumer;

import com.daicy.sequence.SafeSequence;

public class Producer implements Runnable {

	private IDStack<Mantou> stack;

	private static int taskCount;

	private final int id = taskCount++;

	public Producer(IDStack<Mantou> stack) {
		this.stack = stack;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		// while (true) {
		while (!Thread.interrupted()) {
			try {
				int id = SafeSequence.getNext();
				Mantou mantou = new Mantou(id);
				stack.push(mantou);
				System.out.println("P" + this.id + " 生产了 mantou" + mantou);
				// Thread.sleep((long) (Math.random() * 1000));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("P" + this.id + " 生产了 tttmantou");
				break;
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java码界探秘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值