zoukankan      html  css  js  c++  java
  • Java Blocking Queue

    //Listing 8-1. The Blocking Queue Equivalent of Chapter 3’s PC Application
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    public class A
    {
        public static void main(String[] args)
        {
            final BlockingQueue<Character> bq;
            bq = new ArrayBlockingQueue<Character>(26);
            final ExecutorService executor = Executors.newFixedThreadPool(2);
            Runnable producer = () ->
            {
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    try
                    {
                        bq.put(ch);
                        System.out.printf("%c produced by " + "producer.%n", ch);
                    }
                    catch (InterruptedException ie)
                    {
                    }
                }
            };        
            executor.execute(producer);
            
            Runnable consumer = () ->
            {
                char ch = '';
                do
                {
                    try
                    {
                        ch = bq.take();
                        System.out.printf("%c consumed by " + "consumer.%n", ch);
                    }
                    catch (InterruptedException ie)
                    {
                    }
                }
                while (ch != 'Z');
                executor.shutdownNow();
            };
            executor.execute(consumer);
        }    
    }
  • 相关阅读:
    实用产品规划
    产品经理对用户的调研
    产品经理用户研究
    竞品分析方案
    产品竞品分析
    Mybatis Plus
    shiro
    Spring cloud
    Spring Boot
    Redis入门(二)
  • 原文地址:https://www.cnblogs.com/rojas/p/5382598.html
Copyright © 2011-2022 走看看