Java Multi-Threading and concurrency Library Advanced Application Learning Note 16-22 Lesson + face question

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.