• 技术文章 >java >java基础

    java中SynchronousQueue是什么意思

    小妮浅浅小妮浅浅2021-02-08 19:27:32原创2865

    1

    <br>

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1.概念

    SynchronousQueue是一个队列长度为 0 的 BlockingQueue,这样只要上一个入队列的生产者的消息没被消费,之后的生产者就必须等待。如果要保证生产者先后顺序,则需要设置为公平模式。

    2.特点

    1)内部容量是0

    2)每次删除操作都要等待插入操作

    3)每次插入操作都要等待删除操作

    4)一个元素,一旦有了插入线程和移除线程,那么很快由插入线程移交给移除线程,这个容器相当于通道,本身不存储元素

    5)在多任务队列,是最快的处理任务方式。

    3.实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    package com.example.demo.threadnew;

      

    import java.util.Random;

    import java.util.concurrent.SynchronousQueue;

    import java.util.concurrent.TimeUnit;

      

    public class QueueT {

      

        public static void main(String[] args) throws InterruptedException {

            SynchronousQueue<Integer> queue = new SynchronousQueue<Integer>();

      

            new Product(queue).start();

            new Customer(queue).start();

        }

      

        static class Product extends Thread {

      

            SynchronousQueue<Integer> queue;

      

            public Product(SynchronousQueue<Integer> queue) {

                this.queue = queue;

            }

      

            @Override

            public void run() {

                while (true) {

                    int rand = new Random().nextInt(1000);

                    System.out.println("生产了一个产品:" + rand);

                    System.out.println("等待三秒后运送出去...");

                    try {

                        TimeUnit.SECONDS.sleep(3);

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                    try {

                        queue.put(rand);

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

      

                    System.out.println(queue.isEmpty());

                }

            }

        }

      

        static class Customer extends Thread {

      

            SynchronousQueue<Integer> queue;

      

            public Customer(SynchronousQueue<Integer> queue) {

                this.queue = queue;

            }

      

            @Override

            public void run() {

                while (true) {

                    try {

                        System.out.println("消费了一个产品:" + queue.take());

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                    System.out.println("------------------------------------------");

                }

            }

        }

      

    }

    以上就是java中SynchronousQueue的有关介绍,作为一种阻塞队列,可以说对待任务的处理是非常认真的,一定会保证之前经手任务的完成。对这种队列感兴趣的,快试试代码的使用操作吧。

    专题推荐:java synchronousqueue
    上一篇:java中ConcurrentLinkedQueue的入队 下一篇:java中SynchronousQueue的原理

    相关文章推荐

    • java ConcurrentLinkedQueue是什么• ConcurrentLinkedQueue在java的原理探究• java中ConcurrentLinkedQueue的入队

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网