多线程学习——消费者生产者(1)
2013-10-06 22:15
218 查看
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; } } } }
相关文章推荐
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- java多线程学习生产者消费者
- 多线程学习(九)并发协作-生产者消费者模型
- 多线程学习——消费者生产者(2)
- 学习日记1105--多线程间的通信(生产者消费者)(1)
- Java多线程学习之 BlockingQueue实现生产者和消费者模型
- 多线程学习Demo注解(3)——生产者和消费者
- Java基础学习6_多线程(线程间通信--生产者消费者)
- Java多线程学习之生产者消费者问题(一)
- 训练营学习笔记 java多线程(五)生产者/消费者
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- 马士兵-多线程学习第05课 生产者消费者问题
- 学习多线程的生产者和消费者系列的相关实践
- C++多线程学习:生产者消费者问题
- Java基础学习__多线程(线程间通信--生产者消费者JDK5.0升级版)
- 9. 多线程 Part 4 生产者及消费者模式 --- 学习笔记
- C#多线程学习3—— 生产者和消费者
- java多线程学习之生产者与消费者(二)
- Linux C/C++多线程学习:生产者消费者问题
- java学习多线程之生产者消费者