Java.util.concurrent.Exchanger Application Example and principle analysis--Reprint
PackageThread;ImportJava.util.Random;ImportJava.util.concurrent.Exchanger;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classExchangertest { Public Static voidMain (string[] args) {Executorservice Executorservice=Executors.newcachedthreadpool (); FinalExchanger<string> exchanger=NewExchanger<string>(); Executorservice.execute (NewRunnabletest ("A", exchanger)); Executorservice.execute (NewRunnabletest ("B", exchanger)); }}classRunnabletestImplementsrunnable{PrivateString data; PrivateExchanger<string>Exchanger; PublicRunnabletest (String data,exchanger<string>Exchanger) { This. data=data; This. exchanger=Exchanger; } @Override Public voidrun () {Try{System.out.println (Thread.CurrentThread (). GetName ()+ "Data:" +data); Thread.Sleep (NewRandom (). Nextint (1000)); String Swapdata=exchanger.exchange (data); System.out.println (Thread.CurrentThread (). GetName ()+ "have swap data" +swapdata); } Catch(interruptedexception e) {e.printstacktrace (); } } }
Java concurrency programming: blocking queues
Note the way to use the blocking queue itself
The Put method is used to deposit elements to the end of the queue, and if the queues are full, wait;
The Take method is used to fetch elements from the first team, and if the queue is empty, wait;
The Offer method is used to deposit elements to the end of the queue, and if the queues are full, wait for a certain amount of time, and if the time period is reached, false if the insertion is not successful;
The poll method is used to take elements from the first team, and if the queue is empty, it waits for a certain amount of time, and returns null if the time period is reached, otherwise returns the acquired element;
Java Concurrency Programming: Concurrenthashmap of Concurrent Containers (reprint) Java Concurrency Programming: copyonwritearraylist of Concurrent Containers (reprint)
Java Multi-Threading and concurrency Library Advanced Application Learning Note 16-22 Lesson + face question