Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
Here is an example that demonstratesExchanger. It creates two threads. One thread
creates an empty buffer that will receive the data put into it by the second thread. Thus,
the first thread exchanges an empty thread for a full one.

// An example of Exchanger.

import java.util.concurrent.Exchanger;

class ExgrDemo {
public static void main(String args[]) {
Exchanger<String> exgr = new Exchanger<String>();

new UseString(exgr);
new MakeString(exgr);
}
}

// A Thread that constructs a string.
class MakeString implements Runnable {
Exchanger<String> ex;
String str;

MakeString(Exchanger<String> c) {
ex = c;
str = new String();

new Thread(this).start();
}

public void run() {
char ch = 'A';

for(int i = 0; i < 3; i++) {

// Fill Buffer
for(int j = 0; j < 5; j++)
str += ch++;

try {
// Exchange a full buffer for an empty one.
str = ex.exchange(str);
} catch(InterruptedException exc) {
System.out.println(exc);
}
}
}
}

// A Thread that uses a string.
class UseString implements Runnable {
Exchanger<String> ex;
String str;

800 Part II: The Java Library

Free download pdf