In this thread concurrency tutorial we will learn what is java.util.concurrent.Exchanger in java with program and examples. Programs to demonstrate usage of Exchanger in thread concurrency in java.
Contents of page :
- 1) What is java.util.concurrent.Exchanger in java?
- 1.1) Exchanger’s constructor in java >
- Exchanger(int parties)
- 1.2) Exchanger’s exchange() method has 2 forms :
- V exchange(V x)
- V exchange(V x, long timeout, TimeUnit unit)
- 2) Example/ Program to demonstrate usage of Exchanger>
- 2.1) Let’s discuss output in detail, to get better understanding of Exchanger usage in program >
- 3) Application of Exchanger in real world >
1) What is java.util.concurrent.Exchanger in java?
Exchanger enables two threads to exchange their data between each other. Exchanger can be handy in solving Producer Consumer pattern where Producer and consumer threads can exchange their data.
1.1) Exchanger’s constructor in java >
- Exchanger(int parties)
Creates a new Exchanger object.
1.2) Exchanger’s exchange() method has 2 forms in java :
- V exchange(V x)
exchange() method enables two threads to exchange their data between each other in java.
If current thread is first one to call exchange() method then it will until one of following things happen >
- Some other thread calls exchange() method in java, or
- Some other thread interrupts the current thread in java, or
If some other thread has already called exchanger() method then it resumes its execution and following things happen -
- waiting thread is resumed and receives data from current thread in java.
- current thread receives data from that waiting thread and it returns immediately in java.
- V exchange(V x, long timeout, TimeUnit unit)
exchange() method enables two threads to exchange their data between each other in java.
If current thread is first one to call exchange() method then it will until one of following things happen in java >
- Some other thread calls exchange() method in java, or
- Some other thread interrupts the current thread in java, or
- The specified timeout elapses in java.
If some other thread has already called exchanger() method then it resumes its execution and following things happen -
- waiting thread is resumed and receives data from current thread in java.
- current thread receives data from that waiting thread and it returns immediately in java.
2) Example/ Program to demonstrate usage of Exchanger in java>
import java.util.concurrent.Exchanger;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExchangerExample {
public static void main(String[] args) {
Exchanger<String> exchanger=new Exchanger<String>();
System.out.println("Exchanger has been created");
Producer prod=new Producer(exchanger);
Consumer cons=new Consumer(exchanger);
Thread prodThread=new Thread(prod,"prodThread");
Thread consThread=new Thread(cons,"consThread");
prodThread.start(); //start producer thread.
consThread.start(); //start consumer thread.
}
}
/**
* Producer Class.
*/
class Producer implements Runnable{
Exchanger<String> exchanger;
String str;
Producer(Exchanger<String> exchanger){
str=new String();
this.exchanger=exchanger;
}
@Override
public void run(){
for(int i=1;i<=5;i++){
str+=i;
System.out.println("Produced : "+i);
try {
str= exchanger.exchange(str);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* Consumer Class.
*/
class Consumer extends Thread{
Producer prod;
Exchanger<String> exchanger;
String str;
Consumer(Exchanger<String> exchanger){
this.exchanger=exchanger;
}
public void run(){
for(int i=0; i<5;i++){
try {
str= exchanger.exchange(new String());
System.out.println("CONSUMED : " + str );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/* OUTPUT
Exchanger has been created
Produced : 1
CONSUMED : 1
Produced : 2
CONSUMED : 2
Produced : 3
CONSUMED : 3
Produced : 4
CONSUMED : 4
Produced : 5
CONSUMED : 5
*/
|
2.1) Let’s discuss output in detail, to get better understanding of Exchanger usage in program in java >
Note : I have mentioned output in green text.
Exchanger has been created
Exchanger is created, which will enable Producer and consumer threads to exchange their data.
Produced : 1
Producer thread has produced and called exchanger() method, now it will wait for consumer thread to call exchange() method.
Now, [str in producerThread=”1”] &
[str in consumerThread=””]
CONSUMED : 1
Consumer thread has called exchanger() method and following things happens >
- waiting thread (producerThread) is resumed and receives data from current thread (consumerThread).
- current thread(consumerThread) receives data from that waiting thread(producerThread) and it returns immediately.
[Now, str in consumerThread=”1”] &
[Now, str in producerThread=””]
Produced : 2
CONSUMED : 2
Produced : 3
CONSUMED : 3
Produced : 4
CONSUMED : 4
Produced : 5
CONSUMED : 5
3) Application of Exchanger in real world >
Exchanger can be handy in solving Producer Consumer pattern where Producer and consumer threads can exchange their data. Program to demonstrate usage of Exchanger helps us in solving consumer producer problem.
SUMMARY>
In this thread concurrency tutorial we learned what is java.util.concurrent.Exchanger in java with program and examples. We also wrote programs to demonstrate usage of Exchanger in thread concurrency in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
java.util.concurrent.Executor framework in java >
Executor and ExecutorService framework in java
java.util.concurrent.Phaser in java >
Phaser in java
Thread concurrency Interviews in java >