Most abused code written in more than a year. You must review Java concurrency carefully. After a night of testing all ran through, hereby commemorate, in order to encourage!
Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.concurrent.ArrayBlockingQueue;Importjava.util.concurrent.locks.Condition;ImportJava.util.concurrent.locks.Lock;ImportJava.util.concurrent.locks.ReentrantLock;/*** Implement resizable blocking queue, support data migration balance Reader,writer read suction speed, maximum throughput *@authorHanjie **/ Public classRecordbuffer { Public Static FinalRecord Close_record =NewRecord () {@Override PublicObject GetColumnValue (String columnName) {//TODO auto-generated Method Stub return NULL; } }; Public Static FinalRecord Switch_queue_record =NewRecord () {@Override PublicObject GetColumnValue (String columnName) {//TODO auto-generated Method Stub return NULL; } }; PublicLock Switchingqueuelock =NewReentrantlock (); PublicCondition readerswitched =switchingqueuelock.newcondition (); PublicCondition writerswitched =switchingqueuelock.newcondition (); PublicCondition switchfinished =switchingqueuelock.newcondition (); Public volatile BooleanReaderswitchsuccess =true; Public volatile BooleanWriterswitchsuccess =true; Public volatile BooleanSwitchingqueue =false; Public volatile Booleanclosed =false; Private volatileArrayblockingqueue<record>queue; PrivateTaskcounter Taskcounter; PublicRecordbuffer (Taskcounter Taskcounter,intsize) { This. Queue =NewArrayblockingqueue<record>(size); This. Taskcounter =Taskcounter; } Public voidResizeintnewSize) { Try { if(closed) {return; } switchingqueuelock.lock (); Try { //double check, it is impossible for writer to receive Closed_record has exited. Writerswitched.await () will hang if(closed) {return; } This. Switchingqueue =true; Arrayblockingqueue<Record> Oldqueue =queue; Queue=NewArrayblockingqueue<record>(newSize); This. readerswitchsuccess =false; This. writerswitchsuccess =false; //Save Writer First, maybe writer just block to take, failure is OK, indicating that the old queue is not empty, writer will not block to takeOldqueue.offer (Switch_queue_record); while(!writerswitchsuccess) {writerswitched.await (); } //writer switches the queue first, then reader may block on the last put, empty the old queue save reader, let it wake up smoothlyTransferoldqueuerecordstonewqueue (Oldqueue); while(!readerswitchsuccess) {readerswitched.await (); } //the front of the empty, just hit the reader to put the last one, non-blocking emptying action there is a residue last putTransferoldqueuerecordstonewqueue (Oldqueue); This. Switchingqueue =false; This. Switchfinished.signalall (); } finally{switchingqueuelock.unlock (); } } Catch(interruptedexception e) {thread.currentthread (). interrupt (); Throw NewRuntimeException (e); } } Private voidTransferoldqueuerecordstonewqueue (arrayblockingqueue<record>oldqueue)throwsinterruptedexception {List<Record> oldrecords =NewArraylist<record>(Oldqueue.size ()); Record Record=NULL; while(record = Oldqueue.poll ())! =NULL) {Oldrecords.add (record); } //transfer the remaining records of the old queue to the new queue for(inti = 0; I < oldrecords.size (); i++) {queue.put (Oldrecords.get (i)); } } Public voidClose () { This. closed =true; Switchingqueuelock.lock (); Try { //If you are switching queues, wait for the switch to finish before sending the last close while(switchingqueue) {switchfinished.await (); } This. Queue.put (Close_record); } Catch(interruptedexception e) {thread.currentthread (). interrupt (); Throw NewRuntimeException (e); } finally{switchingqueuelock.unlock (); } } Public voidput (record record) {Try { if(!Queue.offer (record)) {Taskcounter.incrbufferfullcount (); if(!readerswitchsuccess) {notifyreaderswitchsuccess (); } queue.put (record); } taskcounter.incrreadcount (); } Catch(interruptedexception e) {thread.currentthread (). interrupt (); Throw NewRuntimeException (e); } } Private voidnotifyreaderswitchsuccess () {System.out.println ("Reader Switch"); Switchingqueuelock.lock (); Try{readerswitchsuccess=true; Readerswitched.signalall (); } finally{switchingqueuelock.unlock (); } } PublicRecord Take () {Try{record Record=Queue.poll (); //If you get the switchover record, the switch queue retries if(Record = =Switch_queue_record) { if(!writerswitchsuccess) {notifywriterswitchsuccess (); } Record=Queue.poll (); } if(Record = =NULL) {taskcounter.incrbufferemptycount (); //call take to check if the switch is in progress and ensure that you get the new queue if(!writerswitchsuccess) {notifywriterswitchsuccess (); } Record=Queue.take (); //If it's unfortunate that you're switching between take blocking, you can only send a toggle record to wake it up . if(Record = =Switch_queue_record) { if(!writerswitchsuccess) {notifywriterswitchsuccess (); } Record=Queue.take (); } } if(Record = =Close_record) { if(!writerswitchsuccess) {notifywriterswitchsuccess (); } return NULL; } taskcounter.incrwritecount (); returnrecord; } Catch(interruptedexception e) {thread.currentthread (). interrupt (); Throw NewRuntimeException (e); } } Private voidnotifywriterswitchsuccess () {System.out.println ("Writer Switch"); Switchingqueuelock.lock (); Try{writerswitchsuccess=true; Writerswitched.signalall (); } finally{switchingqueuelock.unlock (); } }}
Java Scalable blocking Queue implementation