LinkedBlockingQueue_demo

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 ");

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值