-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPC.java
37 lines (34 loc) · 1.13 KB
/
IPC.java
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
import java.io.*;
public class IPC {
public static void main(String[] args) throws IOException{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in); // pass in object to connect pipes
Thread t1 = new Thread(()->{
try {
out.write("Hello world".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
Thread t2 = new Thread(()->{
byte[] bytes = new byte[1024];
int read;
try {
read = in.read(bytes);
System.out.println(new String(bytes,0,read));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
t1.start();
t2.start();
}
}