-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipedCharacters.java
More file actions
82 lines (65 loc) · 1.64 KB
/
Copy pathPipedCharacters.java
File metadata and controls
82 lines (65 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package threadbook.ch08;
import java.io.*;
public class PipedCharacters extends Object {
public static void writeStuff(Writer rawOut) {
try {
BufferedWriter out = new BufferedWriter(rawOut);
String[][] line = {
{ "Java", "has", "nice", "features." },
{ "Pipes", "are", "interesting." },
{ "Threads", "are", "fun", "in", "Java." },
{ "Don't", "you", "think", "so?" }
};
for ( int i = 0; i < line.length; i++ ) {
String[] word = line[i];
for ( int j = 0; j < word.length; j++ ) {
if ( j > 0 ) {
// put a space between words
out.write(" ");
}
out.write(word[j]);
}
// mark the end of a line
out.newLine();
}
out.flush();
out.close();
} catch ( IOException x ) {
x.printStackTrace();
}
}
public static void readStuff(Reader rawIn) {
try {
BufferedReader in = new BufferedReader(rawIn);
String line;
while ( ( line = in.readLine() ) != null ) {
System.out.println("read line: " + line);
}
System.out.println("Read all data from the pipe");
} catch ( IOException x ) {
x.printStackTrace();
}
}
public static void main(String[] args) {
try {
final PipedWriter out = new PipedWriter();
final PipedReader in = new PipedReader(out);
Runnable runA = new Runnable() {
public void run() {
writeStuff(out);
}
};
Thread threadA = new Thread(runA, "threadA");
threadA.start();
Runnable runB = new Runnable() {
public void run() {
readStuff(in);
}
};
Thread threadB = new Thread(runB, "threadB");
threadB.start();
} catch ( IOException x ) {
x.printStackTrace();
}
}
}