Java Exchanger Example

By Arvind Rai, November 16, 2023
Exchanger class is introduced in Java 5. Exchanger class exchanges objects between the threads at any synchronization point. Exchanger has only one method as exchange().

Example

ExchangerTest.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExchangerTest {
      Exchanger<List<String>> exchanger = new Exchanger<List<String>>();
	  List<String>  exchnagerList= new ArrayList<String>();
	   class AddList implements Runnable {
	     public void run() {
	       try {
	         while (true) {
	           exchnagerList.add("1");
	           if (exchnagerList.size()==1){
	        	   exchnagerList = exchanger.exchange(exchnagerList);
	        	   
	           }
	         }
	       } catch (InterruptedException ex) { System.out.println(ex); }
	     }
	   }
	   class SubtractList implements Runnable {
	     public void run() {
	       try {
	         while (true) {
	        	 exchnagerList.remove("1");
	        	 if (exchnagerList.size()==0){
	        	   exchnagerList = exchanger.exchange(exchnagerList);
	        	   
	                 }
	         }
	       } catch (InterruptedException ex) { System.out.println(ex);}
	     }
	   }
	   public static void main(String... args) {
		   final ExecutorService exService = Executors.newFixedThreadPool(2);
		   ExchangerTest ob = new ExchangerTest();
		   exService.execute(ob.new SubtractList());
		   exService.execute(ob.new AddList());
	   }
} 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us