JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Exchanger. Show all posts
Showing posts with label Exchanger. Show all posts

Java Exchanger example

8:55 AM
Exchanger class is introduced with java 1.5 with other classes such ConcurrentHashMap, CountDownLatch, Semaphores. Exchanger class is used to exchange object between two threads. Exchanger simply waits until two separate threads calls exchange method, when it happens, it exchanges data supplied by threads.Two threads can pair and swap objects between them. Exchanger class may be useful in genetic algorithms or pipeline design. It has two overloaded version of exchange method. exchange(V x):  It waits for another thread to arrive at exchange point and exchange object with that thread. exchange(V x, long timeout, TimeUnit unit):  It waits for another thread for  specific time interval  provided in the method and exchange object with that thread. Let’s understand with help of example: We have two threads i.e. Producer and Consumer and they will exchange Country objects. Producer will create Country objects and Consumer will return dummy country objects. Let’s

.