Blocking queue -Synchronous

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/longgeqiaojie304/article/details/91348684

Blocking queue -Synchronous

Manufacturers face questions:

We all know that ArrayList is not thread-safe, please write coding unsafe and gives the case a solution?

Lock fair / unfair lock / reentrant lock / recursive lock / spin locks talk about your understanding? Please handwriting a spin lock.

CountDownLatch, CyclicBarrier, Semaphore used it?

Blocking queue know?

Thread pool used it? ThreadPoolExecutor talk about your understanding?

Thread pool used it? Production How do you set reasonable parameters?

Deadlock coding and positioning analysis?

1, what is blocking queue -Synchronous that?

    Synchronous blocking queue is a queue element is not stored, i.e., a single element queue.

2, Synchronous blocking queue code verification

package com.wwl.juc;

 

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.SynchronousQueue;

import java.util.concurrent.TimeUnit;

 

/**

 * SynchronousBlocking is not a blocking queue memory elements, i.e. a single element queue

 */

public class SynchronousQueueDemo {

    public static void main(String[] args) {

        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

        // producer thread put operation

        new Thread(() -> {

            try {

                System.out.println(Thread.currentThread().getName() + "\t put one");

                blockingQueue.put("one");

 

                System.out.println(Thread.currentThread().getName() + "\t put two");

                blockingQueue.put("two");

 

                System.out.println(Thread.currentThread().getName() + "\t put three");

                blockingQueue.put("three");

            } catch (InterruptedException e) {

                e.printStackTrace ();

            }

        }, "t1").start();

 

        // consumer thread take operation

        new Thread(()->{

            try {

                TimeUnit.SECONDS.sleep(5);

                System.out.println(Thread.currentThread().getName() + "\t take one");

                blockingQueue.take();

 

                TimeUnit.SECONDS.sleep(5);

                System.out.println(Thread.currentThread().getName() + "\t take two");

                blockingQueue.take();

 

                TimeUnit.SECONDS.sleep(5);

                System.out.println(Thread.currentThread().getName() + "\t take three");

                blockingQueue.take();

            } catch (InterruptedException e) {

                e.printStackTrace ();

            }

        },"t2").start();

    }

}

Program execution results are as follows: since consumers take a thread element every 5 seconds, so a producer thread element 5 seconds added to the queue is blocked every Synchronous.

First printing:

Waiting for the first five seconds to print:

Wait for the second five seconds printing:

Wait for a third five seconds to print:

 

Guess you like

Origin blog.csdn.net/longgeqiaojie304/article/details/91348684