2015-04-20 15:36:01 -07:00
|
|
|
//: concurrency/PipedIO.java
|
|
|
|
// Using pipes for inter-task I/O
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
import static net.mindview.util.Print.*;
|
|
|
|
|
|
|
|
class Sender implements Runnable {
|
|
|
|
private Random rand = new Random(47);
|
|
|
|
private PipedWriter out = new PipedWriter();
|
|
|
|
public PipedWriter getPipedWriter() { return out; }
|
2015-05-05 11:20:13 -07:00
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
while(true)
|
|
|
|
for(char c = 'A'; c <= 'z'; c++) {
|
|
|
|
out.write(c);
|
|
|
|
TimeUnit.MILLISECONDS.sleep(rand.nextInt(500));
|
|
|
|
}
|
|
|
|
} catch(IOException e) {
|
|
|
|
print(e + " Sender write exception");
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
print(e + " Sender sleep interrupted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Receiver implements Runnable {
|
|
|
|
private PipedReader in;
|
|
|
|
public Receiver(Sender sender) throws IOException {
|
|
|
|
in = new PipedReader(sender.getPipedWriter());
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
while(true) {
|
|
|
|
// Blocks until characters are there:
|
|
|
|
printnb("Read: " + (char)in.read() + ", ");
|
|
|
|
}
|
|
|
|
} catch(IOException e) {
|
|
|
|
print(e + " Receiver read exception");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PipedIO {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
Sender sender = new Sender();
|
|
|
|
Receiver receiver = new Receiver(sender);
|
|
|
|
ExecutorService exec = Executors.newCachedThreadPool();
|
|
|
|
exec.execute(sender);
|
|
|
|
exec.execute(receiver);
|
|
|
|
TimeUnit.SECONDS.sleep(4);
|
|
|
|
exec.shutdownNow();
|
|
|
|
}
|
2015-05-18 23:05:20 -07:00
|
|
|
} /* Output: (65% Match)
|
2015-04-20 15:36:01 -07:00
|
|
|
Read: A, Read: B, Read: C, Read: D, Read: E, Read: F, Read: G, Read: H, Read: I, Read: J, Read: K, Read: L, Read: M, java.lang.InterruptedException: sleep interrupted Sender sleep interrupted
|
|
|
|
java.io.InterruptedIOException Receiver read exception
|
|
|
|
*///:~
|