zoukankan      html  css  js  c++  java
  • java 双端队列 Deque

    简单的实现生产者消费者模式。

    package com.citi.test.mutiplethread.demo0503;
    
    import java.util.Arrays;
    import java.util.UUID;
    import java.util.concurrent.LinkedBlockingDeque;
    
    public class ProducerDeque implements Runnable{
        private LinkedBlockingDeque<String> deque;
        
        public ProducerDeque(LinkedBlockingDeque<String> deque) {
            this.deque=deque;
        }
    
        @Override
        public void run() {
            while(true){
                try {
                    String str=UUID.randomUUID().toString();
                    System.out.println(Thread.currentThread().getName()+"生产了:"+str);
                    deque.putFirst(str);
                    System.out.println(Thread.currentThread().getName()+"生产了:"+Arrays.toString(deque.toArray()));
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }
    package com.citi.test.mutiplethread.demo0503;
    
    import java.util.concurrent.LinkedBlockingDeque;
    
    public class ConsumerDeque implements Runnable{
    
        private LinkedBlockingDeque<String> deque;
        public ConsumerDeque(LinkedBlockingDeque<String> deque) {
            this.deque = deque;
        }
    
        @Override
        public void run() {
            while(true){
                try {
                    System.out.println(Thread.currentThread().getName()+"消费了:"+deque.takeLast());
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
    
    }
    package com.citi.test.mutiplethread.demo0503;
    
    import java.util.concurrent.LinkedBlockingDeque;
    
    public class LinkedBlockingDequeTest {
        public static void main(String[] args) {
            LinkedBlockingDeque<String> producerDeque=new LinkedBlockingDeque<String>();
            
            new Thread(new ProducerDeque(producerDeque)).start();
            new Thread(new ProducerDeque(producerDeque)).start();
            new Thread(new ProducerDeque(producerDeque)).start();
            new Thread(new ProducerDeque(producerDeque)).start();
            
            new Thread(new ConsumerDeque(producerDeque)).start();
        }
    }

  • 相关阅读:
    [机器学习]特征归一化
    【图像基础】图像不变性特征HU矩和Zernike矩
    [error]OpenCV Error: Assertion failed (ssize.width > 0 && ssize.height > 0) in resize, file modules/imgproc/src/resize.cpp, line 3289
    【linux基础】如何确定系统使用的Eigen库的版本
    计算机视觉会议及团队
    219. Contains Duplicate II
    217. Contains Duplicate
    206. Reverse Linked List
    gdb debug
    【leetcode】205-Isomorphic Strings
  • 原文地址:https://www.cnblogs.com/liumy/p/11587576.html
Copyright © 2011-2022 走看看