Java programming thread synchronization tool Exchanger use instance resolution

  • 2021-01-25 07:32:49
  • OfStack

This article mainly studies the use of Exchanger programming thread synchronization tool Java, let's take a look at the details.

If two threads need to exchange information, such as a piece of data or space, they need to use Exchanger class. Exchanger provides a convenient way for threads to exchange information. It can be used as a synchronization point for two threads to exchange objects, only when each thread is entering exchange () Method can accept the object given by another thread when it returns.

Only two threads can exchange data at a time, or if there are multiple threads, only two can exchange data. Here is a popular example: 1 hand over money 1 first delivery!


public class ExchangerTest {
	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		final Exchanger exchanger = new Exchanger();
		// define 1 An exchange object, used to exchange data 
		// open 1 The thread performs the task 
		service.execute(new Runnable(){
			@Override
			      public void run() {
				try {
					String data1 = " heroin ";
					System.out.println(" thread " + Thread.currentThread().getName() 
					              + " Is taking the drugs. " + data1 + " Take it out ");
					Thread.sleep((long)(Math.random()*10000));
					// To transmit the data to be exchanged exchange Method, then blocked, waiting for another 1 The thread is swapped with it. Returns the exchanged data 
					String data2 = (String)exchanger.exchange(data1);
					System.out.println(" thread " + Thread.currentThread().getName() + 
					          " He traded it for heroin " + data2);
				}
				catch(Exception e){
				}
				finally {
					service.shutdown();
					System.out.println(" Deal over, take the money and run! ");
				}
			}
		}
		);
		// On the other 1 The thread performs the task 
		service.execute(new Runnable(){
			@Override
			      public void run() {
				try {
					String data1 = "300 wan ";
					System.out.println(" thread " + Thread.currentThread().getName() + 
					          " Is the " + data1 +" Take it out ");
					Thread.sleep((long)(Math.random()*10000));
					String data2 = (String)exchanger.exchange(data1);
					System.out.println(" thread " + Thread.currentThread().getName() + 
					          " with 300 All got " + data2);
				}
				catch(Exception e){
				}
				finally {
					service.shutdown();
					System.out.println(" Deal over, take the heroin and run! ");
				}
			}
		}
		);
	}
}

From the code I could see two people dealing drugs... Here's how the deal turned out:

[

Thread pool-1-thread-1 is taking the drug heroin out
Thread pool-1-thread-2 is taking out 3 million
Thread pool-1-thread-2 got heroin for $3 million
Thread pool-1-thread-1 got $3 million for heroin
Deal over, take the heroin and run!
Deal over, take the money and run!

]

Run is fast, from the results of the operation, it does achieve the exchange of data, this is just the exchange of a basic type of data, its real use is not limited to this, such as we can exchange an object, this is useful, JDK official mentioned an advanced application:

[

Use Exchanger to swap buffers between threads, so that, when needed, the thread filling the buffer gets a newly empty buffer and passes the filled buffer to the thread vacuuming the buffer.

]

This depends on the actual situation, the idea is the same as the above 1, in practice must define a buffer class, and then between two threads is the buffer class can be exchanged, as to how the class implementation, depends on the actual situation. So much for using Exchanger

conclusion

This article is about the use of Java thread synchronization tool Exchanger instance resolution of all content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: