In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand the thread safety of ArrayBlockingQueue". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The thread safety of ArrayBlockingQueue is guaranteed by the underlying ReentrantLock, so there is no need for additional locking when elements are in and out of the queue. Write a simple piece of code, for example, to illustrate its thread safety in terms of specific use.
ArrayBlockingQueue queue=new ArrayBlockingQueue (7, true, new ArrayList (Arrays.asList (new Integer [] {1pje, 2pje, 3pje, 5pje, 6pje); @ AllArgsConstructorclass Task implements Runnable {String threadName; @ Override public void run () {while (true) {try {System.out.println (threadName+ "take:" + queue.take ());} catch (InterruptedException e) {e.printStackTrace () } private void queueTest () {new Thread (new Task ("Thread 1")) .start (); new Thread (new Task ("Thread 2")) .start ();}
When you create the queue in the code, you put seven elements into it, and then create two threads to pull the elements out of the queue. The operation on the queue is also very simple. Only the dequeuing method take in the operation queue is used. The running result is as follows:
Thread 1 take: 1Thread 2 take: 2Thread 1 take: 3Thread 2 take: 4Thread 1 take: 5Thread 2 take: 6Thread 1 take: 7
It can be seen that in fair mode, two threads alternately perform dequeue operations on the elements in the queue, and there is no repeated fetching, that is, the mutually exclusive access of multiple threads to resource competition is guaranteed. Its process is as follows:
Interviewer: what about its obstruction?
Hydra: OK, let's write a piece of code to illustrate it with an example
Private static void queueTest () throws InterruptedException {ArrayBlockingQueue queue=new ArrayBlockingQueue (3); int size=7; Thread putThread=new Thread (()-> {for (int I = 0; I {for (int I = 0; I)
< size+1 ; i++) { try { Thread.sleep(3000); System.out.println("TakeThread take: "+queue.take()); } catch (InterruptedException e) { e.printStackTrace(); } } }); putThread.start(); Thread.sleep(1000); takeThread.start();} 和第一个例子中的代码不同,这次我们创建队列时只指定长度,并不在初始化时就往队列中放入元素。接下来创建两个线程,一个线程充当生产者,生产产品放入到队列中,另一个线程充当消费者,消费队列中的产品。需要注意生产和消费的速度是不同的,生产者每一秒生产一个,而消费者每三秒才消费一个。执行上面的代码,运行结果如下: PutThread put: 0 - Size:1PutThread put: 1 - Size:2PutThread put: 2 - Size:3TakeThread take: 0PutThread put: 3 - Size:3TakeThread take: 1PutThread put: 4 - Size:3TakeThread take: 2PutThread put: 5 - Size:3TakeThread take: 3PutThread put: 6 - Size:3TakeThread take: 4TakeThread take: 5TakeThread take: 6 来给你画个比较直观的图吧:By analyzing the running results, it can reflect the obstruction of the queue in two aspects:
Queue blocking: when the number of elements in the queue is equal to the length of the queue, the operation of putting elements into the queue will be blocked. When there is an out-of-queue operation to remove the elements from the queue and there is a vacancy in the queue, the queue will be rejoined.
Dequeue blocking: when the element in the queue is empty, the thread performing the dequeue operation will be blocked and will not perform the dequeue operation again until the queue is not empty. In the dequeue thread of the above code, we deliberately set the number of queuing times to add one to the number of elements in the queue, so the thread will be blocked all the time, and the program execution will not end.
Interviewer: can you only use put and take methods? can you tell me about other methods?
Hydra: there are too many methods. Let's briefly summarize the operations related to insert and remove.
Interviewer: I remember the method very clearly. It seems to be a qualified API caller. Let's talk about the principle. Let's talk about the structure of ArrayBlockingQueue first.
Hydra: there are four more important attributes in ArrayBlockingQueue
Final Object [] items;final ReentrantLock lock;private final Condition notEmpty;private final Condition notFull;public ArrayBlockingQueue (int capacity, boolean fair) {if (capacity)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.