Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java conditional queue?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the Java conditional queue". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the Java conditional queue is.

What can you say about the conditional queue?

A conditional queue is a container that carries a set of threads waiting for a priori condition to come true.

The word transcendental condition is creepy, and in vernacular it is a prerequisite for you to do something. It is often shown in the code as a prerequisite that the method you call can be executed. For example, if you want to call the put () method for BlockingQueue, the put method can be called successfully if the blockingQueue is dissatisfied. For full cases, in the synchronous world, you can throw exceptions and you can return a special custom value (you can do better in functional programming). In the world of concurrency, it would be better to be able to block and wait until the queue is dissatisfied. Conditional queues are a key component of this "wait, notify, and run" mechanism.

When you mentioned waiting and notification above, did you immediately think of the familiar sychronized keyword? Because in synchronized, we often use wati (), notify (), notifyAll (). It's pretty good if you think directly about object.

Prior conditions are often associated with the state of the object, because these conditions are ultimately the result of Boolean operations based on certain "object attributes" in the code, which is used to determine whether a priori condition is true. In the concurrent world, these object properties must be synchronized to avoid the problems caused by multithreading competition, which requires a lock to synchronize the states used by these prior conditions. so there is the following logic: a priori condition-- > state-- > lock. In order to test a condition, we must first hold the lock.

Going back to the example of blockingQueue above, we first get the lock of the queue, and then check to see if the queue is full. If the queue is full, we cannot continue to execute put, we need to block live, and then wait for the queue to be notified of dissatisfaction. How can it be realized? Call wait () to release the lock, wait for the notification notify after the condition becomes true, and then continue execution. It is possible to be notified that the condition changes to true, but when the cpu time slot is to be executed, the condition is false again, so the wait is written in while, which is a pattern.

In fact, condition queue has been silently used at this point. When you call wait, the thread enters the conditional queue. When there are other threads notify, it is actually notifying the threads in the condition queue that the conditions have changed, giving them the opportunity to re-check the conditions and continue to run. Here, we can realize that calls to wait () implicitly correspond to waiting for a priori condition (conditional predicate) to be true, while calls to notify/All implicitly correspond to a priori condition.

It is generally known that sycnhronized is closely related to monitor (built-in lock). Before entering the synchronization code block, you must have monitor. In fact, conditional queues are also closely related to locks (or even 'the same' object). In terms of built-in conditional queues, the downside is that calling wait () to put threads in this internal conditional queue means that threads waiting for different "prior conditions" are all in the same queue, that is, different prior conditions share the same internal condition queue. This is not efficient when notifyAll wakes up.

The Condition interface can help us create different condition queues for different prior conditions, so that only the corresponding threads can be awakened. As you can guess from the relationship between locks and conditional queues, the Lock interface provides a way to create conditional queues.

Public interface Lock {Condition newCondition ();...

Here is the interface definition of Condition. You can see that just as Lock is the generalization and display of internal locks, and Condition is the generalization and display of internal condition queues.

Thank you for your reading, the above is the content of "what is the Java conditional queue". After the study of this article, I believe you have a deeper understanding of what the Java conditional queue is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report