blob: f75fe5855ee3b6473adbca5a434f361236c018ee (
plain)
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
|
/* Listing2214.java */
import java.io.*;
class Producer2214
extends Thread
{
private PipedOutputStream pipe;
public Producer2214(PipedOutputStream pipe)
{
this.pipe = pipe;
}
public void run()
{
while (true) {
byte b = (byte)(Math.random() * 128);
try {
pipe.write(b);
System.out.println("Produzent erzeugte " + b);
} catch (IOException e) {
System.err.println(e.toString());
}
try {
Thread.sleep((int)(100*Math.random()));
} catch (InterruptedException e) {
//nichts
}
}
}
}
class Consumer2214
extends Thread
{
private PipedInputStream pipe;
public Consumer2214(PipedInputStream pipe)
{
this.pipe = pipe;
}
public void run()
{
while (true) {
try {
byte b = (byte)pipe.read();
System.out.println(" Konsument fand " + b);
} catch (IOException e) {
System.err.println(e.toString());
}
try {
Thread.sleep((int)(100*Math.random()));
} catch (InterruptedException e) {
//nichts
}
}
}
}
public class Listing2214
{
public static void main(String[] args)
throws Exception
{
PipedInputStream inPipe = new PipedInputStream();
PipedOutputStream outPipe = new PipedOutputStream(inPipe);
Producer2214 p = new Producer2214(outPipe);
Consumer2214 c = new Consumer2214(inPipe);
p.start();
c.start();
}
}
|