In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the Exchanger method of Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Exchanger method of Java".
Brief introduction
Exchanger is a utility class for data exchange between threads. It provides a common point at which two threads can exchange data with each other.
When one thread calls the exchange method, it goes into a waiting state until another thread calls the exchange method, and the two parties complete the data exchange and continue to execute.
Introduction to the use of Exchanger
Exchange (V x): blocks the current thread until another thread calls the exchange method or the current thread is interrupted.
X: the object to be exchanged.
Exchange (V x, long timeout, TimeUnit unit): blocks the current thread until another thread calls the exchange method or the current thread is interrupted or the wait times out.
X: the object to be exchanged.
Timeout: timeout.
Unit: unit of timeout.
The exchange method normally returns the swapped object, and the exchange method returns null when the current thread is interrupted or when the wait times out.
Example 1 students An and B exchange their respective collections of blockbusters
Public class Demo {public static void main (String [] args) {Exchanger stringExchanger = new Exchanger (); Thread studentA = new Thread (()-> {try {String dataA = "A classmate's favorite blockbuster for many years"; String dataB = stringExchanger.exchange (dataA); System.out.println ("A classmate got" + dataB) } catch (InterruptedException e) {e.printStackTrace ();}}); Thread studentB = new Thread (()-> {try {String dataB = "blockbuster collected by classmate B for many years"; String dataA = stringExchanger.exchange (dataB) System.out.println ("classmate B got" + dataA);} catch (InterruptedException e) {e.printStackTrace ();}}); studentA.start (); studentB.start () }} / * * output result: * classmate B got the blockbuster collected by classmate A for many years * classmate A got the blockbuster that classmate B collected for many years * /
Example 2 A classmate was stood up and the deal failed
Public class Demo {public static void main (String [] args) {Exchanger stringExchanger = new Exchanger (); Thread studentA = new Thread (()-> {String dataB = null; try {String dataA = "A classmate's collection of blockbusters for many years"; / / wait up to 5 seconds dataB = stringExchanger.exchange (dataA, 5, TimeUnit.SECONDS) } catch (InterruptedException e) {e.printStackTrace ();} catch (TimeoutException ex) {System.out.println ("wait timeout-TimeoutException");} System.out.println ("A classmate got" + dataB);}); studentA.start () }} / * * output result: * waiting for timeout-TimeoutException * A students got null * / Thank you for your reading. The above is the content of "how to use Java's Exchanger method". After the study of this article, I believe you have a deeper understanding of how to use Java's Exchanger method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.