zoukankan      html  css  js  c++  java
  • 阻塞队列-SynchronousQueue

    1、阻塞队列-Synchronous是什么?

    Synchronous阻塞队列是一个不存储元素的队列,即单个元素队列。

    2、Synchronous阻塞队列代码验证

    public class SynchronousQueueDemo {
        public static void main(String[] args) {
    
            BlockingQueue queue = new SynchronousQueue();
    
            // 生产者线程进行put操作
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName() + "	 put one");
                    queue.put("one");
    
                    System.out.println(Thread.currentThread().getName() + "	 put two");
                    queue.put("two");
    
                    System.out.println(Thread.currentThread().getName() + "	 put three");
                    queue.put("three");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "t1").start();
    
            // 消费者线程进行take操作
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(Thread.currentThread().getName() + "	 take one");
                    queue.take();
    
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(Thread.currentThread().getName() + "	 take two");
                    queue.take();
    
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(Thread.currentThread().getName() + "	 take three");
                    queue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "t2").start();
        }
    }
  • 相关阅读:
    Java 处理cookie的方法
    HTML5的新标签-整体布局
    Git学习文档——文件状态git status
    Css中路径data用法
    python2
    hangfire
    Nginx系列~Nginx服务启动不了
    git形成本地仓库并从远处url拉取
    orcal和sql server中的字符串查找函数
    Eclipse 修改项目名称
  • 原文地址:https://www.cnblogs.com/xhyouyou/p/12465305.html
Copyright © 2011-2022 走看看