LinkedBlockingQueue_demo
2015-12-26 23:21
417 查看
package com.thread.perfect; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; public class ToastOMatic { public static void main(String[] args) throws InterruptedException { ToastQueue dryQueue = new ToastQueue(), butterQueue = new ToastQueue(), finishQueue = new ToastQueue(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new Toaster(dryQueue)); exec.execute(new Butter(dryQueue, butterQueue)); exec.execute(new Jammer(butterQueue, finishQueue)); exec.execute(new Eater(finishQueue)); TimeUnit.MILLISECONDS.sleep(1000); exec.shutdownNow(); } } /** * bean */ class Toast { public enum Status { DRY, BUTTERED, JAMMED } private Status status = Status.DRY; private final int id; public Toast(int idn) { id = idn; } public void butter() { status = Status.BUTTERED; } public void jam() { status = Status.JAMMED; } public Status getStatus() { return status; } public int getId() { return id; } @Override public String toString() { return "Toast [status=" + status + ", id=" + id + "]"; } } /* * 不定长阻塞队列 */ class ToastQueue extends LinkedBlockingQueue<Toast> { private static final long serialVersionUID = 1L; } /** * */ class Toaster implements Runnable { private ToastQueue toastQueue; private int count = 0; private Random random = new Random(47); public Toaster(ToastQueue tq) { toastQueue = tq; } @Override public void run() { try { while (!Thread.interrupted()) { TimeUnit.MILLISECONDS.sleep(100 + random.nextInt(500)); // make Toast Toast t = new Toast(count++); System.out.println(t); // insert into queue toastQueue.add(t); } } catch (InterruptedException e) { System.out.println("Toaster interrupted "); } System.out.println("Toaster off "); } } // Apply butter to toast class Butter implements Runnable { private ToastQueue dryQueue, butterQueue; public Butter(ToastQueue dry, ToastQueue butter) { dryQueue = dry; butterQueue = butter; } @Override public void run() { try { while (!Thread.interrupted()) { // Blocks until next piece of toast is available Toast t = dryQueue.take(); t.butter(); System.out.println(t); butterQueue.put(t); } } catch (InterruptedException e) { System.out.println("Butter interrupted "); } System.out.println("Butter off "); } } // apply jam to butter toast class Jammer implements Runnable { private ToastQueue butterQueue, finishedQueue; public Jammer(ToastQueue bq, ToastQueue fq) { butterQueue = bq; finishedQueue = fq; } @Override public void run() { try { while (!Thread.interrupted()) { // Blocks until next piece of toast is available Toast t = butterQueue.take(); t.jam(); System.out.println(t); finishedQueue.put(t); } } catch (InterruptedException e) { System.out.println("Jammer interrupted "); } System.out.println("Jammer off "); } } // Consume the toast: class Eater implements Runnable { private ToastQueue finishedQueue; private int counter = 0; public Eater(ToastQueue fq) { finishedQueue = fq; } @Override public void run() { try { while (!Thread.interrupted()) { // Blocks until next piece of toast is available Toast t = finishedQueue.take(); // Verify that the toast is comming in order,and that all pieces are getting jammed: if (t.getId() != counter++ || t.getStatus() != Toast.Status.JAMMED) { System.out.println(">>>ERROR : " + t); System.exit(0); } else { System.out.println("OK---> : " + t); } } } catch (InterruptedException e) { System.out.println("Eater interrupted "); } System.out.println("Eater off "); } }
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法
- C#控制台下测试多线程的方法
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- C#如何对多线程、多任务管理(demo)
- C#实现多线程的Web代理服务器实例
- c#实现多线程局域网聊天系统
- PHP使用CURL实现多线程抓取网页