Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Exchanger method of Java

Shulou Source: shulou.com Published: 2022-06-01 12:52:38 10月01日 Update

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!

Tags: Students methods threads blockbusters years objects data learning content situations time examples results output blocking two methods of use units both sides that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi OPPO Reno Shulou Tech Info Microsoft