-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSync.java
More file actions
52 lines (37 loc) · 847 Bytes
/
Sync.java
File metadata and controls
52 lines (37 loc) · 847 Bytes
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
package com.ub.thread;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Condition;
public class Sync {
// volatile int x = 6;
// volatile int y = 0;
int x = 6;
int y = 0;
Thread foo;
Thread bar;
Semaphore sem = new Semaphore(-1);
Boolean fooDone = false;
// Condition fooFinishedcv = new newCondition();
public Sync() {
foo = new Thread(new Foo(),"Foo Thread");
bar = new Thread(new Bar(),"Bar Thread");
foo.start();
bar.start();
System.out.println("In Main ::x = "+x+"::y = "+y);
}
class Foo implements Runnable {
@Override
public void run() {
x++;
y = x;
System.out.println("In FOO ::x = "+x+"::y = "+y);
}
}
class Bar implements Runnable {
@Override
public void run() {
y++;
x+= 3;
System.out.println("In BAR ::x = "+x+"::y = "+y);
}
}
}