-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipedStream.java
More file actions
62 lines (58 loc) · 1.46 KB
/
PipedStream.java
File metadata and controls
62 lines (58 loc) · 1.46 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
package stream;
import java.io.*;
import java.io.InputStream;
class Producer extends Thread{
OutputStream OS;
Producer(OutputStream O){
OS = O;
}
@Override
public void run(){
int count = 0;
while(true){
try{
OS.write(count);
OS.flush();
System.out.println("Produces: "+(count++));
System.out.flush();
Thread.sleep(10);
}
catch(IOException | InterruptedException e){
System.out.println(e);
}
}
}
}
class Consumer extends Thread{
InputStream IS;
Consumer(InputStream O){
IS = O;
}
@Override
public void run(){
int x;
while(true){
try{
x = IS.read();
System.out.println("Consumes: "+(x));
System.out.flush();
Thread.sleep(10);
} catch(IOException | InterruptedException e){
System.out.println(e);
}
}
}
}
public class PipedStream {
public static void main(String[] args) {
PipedInputStream PIS = new PipedInputStream();
PipedOutputStream POS = new PipedOutputStream();
try {
PIS.connect(POS);
}catch (IOException exception){System.out.println(exception);}
Producer P = new Producer(POS);
Consumer C = new Consumer(PIS);
P.start();
C.start();
}
}