Skip to Content
Java in a Nutshell, 5th Edition
book

Java in a Nutshell, 5th Edition

by David Flanagan
March 2005
Intermediate to advanced
1254 pages
104h 21m
English
O'Reilly Media, Inc.
Content preview from Java in a Nutshell, 5th Edition

Name

Exchanger<V>

Synopsis

This class allows two threads to rendezvous and exchange data. This is a generic type, and the type variable V represents the type of data to be exchanged. Each thread should call exchange( ) and pass the value of type V that it wants to exchange. The first thread to call exchange( ) blocks until the second thread calls it. At that point, both threads resume. Both threads receive as their return value the object of type V passed by the other thread. Note that this class also defines a timed version of exchange( ) that throws a TimeoutException if no exchange occurs within the specified timeout interval. Unlike a CountDownLatch, which is a one-shot latch, and CyclicBarrier which can be “broken,” an Exchanger may be reused for any number of exchanges.

public class Exchanger<V> {
// Public Constructors
     public Exchanger( );  
// Public Instance Methods
     public V exchange(V x) throws InterruptedException;  
     public V exchange(V x, long timeout, TimeUnit unit) 
        throws InterruptedException, TimeoutException;  
}
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java in a Nutshell, 8th Edition

Java in a Nutshell, 8th Edition

Benjamin J. Evans, Jason Clark, David Flanagan
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Learning Java, 4th Edition

Learning Java, 4th Edition

Patrick Niemeyer, Daniel Leuck

Publisher Resources

ISBN: 0596007736Supplemental ContentErrata Page