blob: ebd0227153b4bda0ff5003d5cc56752a623780ba (
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
75
76
77
78
79
80
81
82
|
/* Listing2213.java */
import java.util.*;
class Producer2213
extends Thread
{
private Vector v;
public Producer2213(Vector v)
{
this.v = v;
}
public void run()
{
String s;
while (true) {
synchronized (v) {
s = "Wert "+Math.random();
v.addElement(s);
System.out.println("Produzent erzeugte "+s);
v.notify();
}
try {
Thread.sleep((int)(100*Math.random()));
} catch (InterruptedException e) {
//nichts
}
}
}
}
class Consumer2213
extends Thread
{
private Vector v;
public Consumer2213(Vector v)
{
this.v = v;
}
public void run()
{
while (true) {
synchronized (v) {
if (v.size() < 1) {
try {
v.wait();
} catch (InterruptedException e) {
//nichts
}
}
System.out.print(
" Konsument fand "+(String)v.elementAt(0)
);
v.removeElementAt(0);
System.out.println(" (verbleiben: "+v.size()+")");
}
try {
Thread.sleep((int)(100*Math.random()));
} catch (InterruptedException e) {
//nichts
}
}
}
}
public class Listing2213
{
public static void main(String[] args)
{
Vector v = new Vector();
Producer2213 p = new Producer2213(v);
Consumer2213 c = new Consumer2213(v);
p.start();
c.start();
}
}
|