-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntrypoint.java
More file actions
62 lines (46 loc) · 1.07 KB
/
Entrypoint.java
File metadata and controls
62 lines (46 loc) · 1.07 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 com.ub.thread;
public class Entrypoint {
private boolean lock = false;
private Integer sum = 0;
private Integer sum1 = 0;
/**
* @param args
*/
public static void main(String[] args) {
Entrypoint ent = new Entrypoint();
Thread t = new Thread(ent.consumer,"Consumer");
t.start();
Thread t1 = new Thread(ent.producer,"producer");
t1.start();
}
Runnable producer = new Runnable() {
@Override
public void run() {
synchronized (sum) {
System.out.println("In producer producing sum");
for(int i=0;i<200;i++) {
sum1 += 200;
}
lock = true;
sum.notify();
}
}
};
Runnable consumer = new Runnable() {
@Override
public void run() {
synchronized (sum) {
while(lock == false) {
try {
System.out.println("Waiting for sum to complete....");
sum.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("In consumer sum :"+sum1);
}
}
};
}