-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrunchifyProducerConsumer.java
55 lines (49 loc) · 1.1 KB
/
CrunchifyProducerConsumer.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class CrunchifyProducerConsumer {
private static Vector<Object> data = new Vector<Object>();
public static void main(String[] args) {
new Producer().start();
new Consumer().start();
}
public static class Consumer extends Thread {
Consumer() {
super("Consumer");
}
@SuppressWarnings("rawtypes")
@Override
public void run() {
for (;;) {
try {
Thread.sleep(1000);
System.out.println("Object Consumed ################");
} catch (Exception e) {
e.printStackTrace();
}
synchronized (data) {
Iterator it = data.iterator();
while (it.hasNext())
it.next();
}
}
}
}
public static class Producer extends Thread {
Producer() {
super("Producer");
}
@Override
public void run() {
for (;;) {
try {
Thread.sleep(1000);
System.out.println("Object Produced ~~~~~~~~~~~~~~~");
} catch (Exception e) {
e.printStackTrace();
}
data.add(new Object());
if (data.size() > 1000)
data.remove(data.size() - 1);
}
}
}
}