-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreads_05.java
40 lines (39 loc) · 1.58 KB
/
Threads_05.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
class Entry{
private int count = 0;
public synchronized void increment(){
count++;
}
public synchronized int getCount(){
return count;
}
}
public class Threads_05 {
public static void main(String[] args) throws InterruptedException {
// thread sync is a mechanism to share a resource between threads
// in a safe and efficient manner
// it is achieved by using synchronized keyword
// a variable should be accessible by only one thread at one time else it produce wrong results
Entry e = new Entry();
Thread t1 = new Thread(()->{
for(int i =0;i<10000;i++){
e.increment();
}
});
Thread t2 = new Thread(()->{
for(int i = 0; i<10000; i++){
e.increment();
}
});
t1.start();
t2.start();
System.out.println("before " + e.getCount());
t1.join(); // join will do our work
t2.join(); // join joins the thread with main thread
System.out.println("after " + e.getCount());
// lets remove synchronized keyword first then try run our keyword you do the same and run this file
// you will see a clear difference we never reached the final value because one thread interupts another thread so
// 1/4 times (interruption is occuring )
// lets add the synchronized keyword and see if we get final result
// hah, now we got the final result 20000, because threads are getting interrupted by each other
}
}