Exchanger can be regarded as the synchronousqueue of two-way data transmission, that is, no producer and consumer, any two threads can exchange data.
In JDK5, Exchanger is designed as a container with a capacity of 1, holding a waiting thread until another thread arrives to exchange data, then emptying the container until the next incoming thread.
Starting with JDK6, Exchanger uses a segmented idea like Concurrentmap, which provides multiple slots, increasing the throughput of concurrent execution.
Exchanger there is no fair and unfair mode, because there is no queuing situation, as long as there are two of threads can occur data exchange.
See the core approach directly:
PrivateObject Doexchange (Object item,BooleanTimedLongNanos) {Node Me=NewNode (item); //index is the hash value of the thread ID that maps to a value between 0 and Max//in general, Max is 0, so the thread exchange data only uses the first slot,//that is, index is 0, and Max is not 0, see the following loop intindex =Hashindex (); //number of failed CAS operations intFails = 0; for (;;) { //the object stored in the current slot, the nodeObject y; Slot Slots=Arena[index]; //lazy loading, that is, a slot is created only if the slot is null//re-cycle once after lazy loading if(Slot = =NULL) Createslot (index); //there is data in the slot, which means that the thread is waiting for the data to be exchanged//you can then try to reset the slot with CAS (set the object stored in the slot to null)//data exchange with the object stored in the slot and the current thread//notifies the previously awaited thread if the interchange is successful Else if((y = Slot.get ())! =NULL&&slot.compareandset (Y,NULL) ) {Node you=(Node) y; if(You.compareandset (NULL, item)) {Locksupport.unpark (you.waiter); returnYou.item; } //if the slot store object has been reset to NULL, the data exchange failed//This means that the waiting thread's exchange request was canceled.//you will see how to handle this situation when parsing the method code of the wait type } //If there is no storage object in the slot, the first attempt is to store the current thread in the slot//If the storage fails, re-cycle Else if(Y = =NULL&&Slot.compareandset (NULL, ME)) { //An index of 0 means that only the current thread is waiting for the data to be exchanged, so wait directly if(Index = = 0) returnTimed?Awaitnanos (Me, slot, Nanos): Await (me, slot); //the so-called spin wait: is a fixed number of cycles, each count minus one//for a single-core system, spin wait is not done because the single-core//You need to consume CPU while doing wait, other threads are unable to use the CPU, so//waiting is meaningless. A spin value of 2000 in a multicore system would do//2000 cycles. //If there is still no data exchanged after the loop is completed, a//The Cancel object indicates that the request is still canceled and that node is purged from the slotObject v =spinWait (me, slot); if(V! =CANCEL)returnv; //if canceled, create a new node to cancel the previously canceled node for the next loopme =NewNode (item); intm =Max.get (); //Index In addition to 2, narrow the range of slots//and if M is too large, decrease M if(M > (Index >>>= 1)) Max.compareandset (M, M-1); } //allow CAs to fail two times because there are CAs in two else if, so here//A CAs operation that allows two else if has failed Else if(++fails > 1) { intm =Max.get (); //failed more than 3 times, increased m, and re-indexed from M if(Fails > 3 && m < full && max.compareandset (M, M + 1)) Index= m + 1; //when index is less than 0, return to M, re-cycle Else if(--index < 0) Index=m; } } }
This article is about indexing index this piece is not very clear, follow-up will continue to study, timely update.
"Java.util.concurrent package source reading" Exchanger