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

Fair and unfair synchronization queues of SynchronousQueue in java

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is mainly about "Fair and unfair synchronization queue of SynchronousQueue in java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the fair and unfair synchronization queue of SynchronousQueue in java.

SynchronousQueue is divided into fair and unfair synchronization queues

Input true in the construction method, indicating that the fair queue now chooses to implement the entry and exit element FIFO through the method in TransferQueue.

Otherwise, input false and use TransferStack to implement entry and exit elements. There is no fixed order.

Node composition in TransfererStack

Static final class SNode {

Volatile SNode next

Volatile SNode match

Volatile Thread waiter

Object item

Int mode

Volatile SNode head

}

Static {

Try {

UNSAFE = sun.misc.Unsafe.getUnsafe ()

Class k = SNode.class

MatchOffset = UNSAFE.objectFieldOffset

(k.getDeclaredField ("match"))

NextOffset = UNSAFE.objectFieldOffset

(k.getDeclaredField ("next"))

} catch (Exception e) {

Throw new Error (e)

}

}

Implementation of transfer method

E transfer (E e, boolean timed, long nanos) {

/ *

* Basic algorithm is to loop trying one of three actions:

*

* 1. If apparently empty or already containing nodes of same

* mode, try to push node on stack and wait for a match

* returning it, or null if cancelled.

*

* 2. If apparently containing node of complementary mode

* try to push a fulfilling node on to stack, match

* with corresponding waiting node, pop both from

* stack, and return matched item. The matching or

* unlinking might not actually be necessary because of

* other threads performing action 3:

*

* 3. If top of stack already holds another fulfilling node

* help it out by doing its match and/or pop

* operations, and then continue. The code for helping

* is essentially the same as for fulfilling, except

* that it doesn't return the item.

, /

SNode s = null; / / constructed/reused as needed

Int mode = (e = = null)? REQUEST: DATA

For (;;) {

SNode h = head

If (h = = null | | h.mode = = mode) {/ / empty or same-mode

If (timed & & nanos

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