-
Notifications
You must be signed in to change notification settings - Fork 0
/
Race_Condition_threads.java
44 lines (38 loc) · 1.43 KB
/
Race_Condition_threads.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
class Counter {
public int count = 0;
public synchronized void increment() {// we can use synchronized to solve this problem if t1 is incrementing then t2
// should not be allowed to increment
count += 1;
}
}
public class Race_Condition_threads {
public static void main(String[] args) throws InterruptedException {
// threads
// mutation - we can change something, primitive data types
// strings are immutable
// suppose you have two threads t1 changes value of variable and t2 also changes
//
Counter c = new Counter();
Runnable obj1 = () -> {
for (int i = 1; i <= 1000; i++) {
// System.out.println("hello");
c.increment();
}
};
Runnable obj2 = () -> {
for (int i = 1; i <= 1000; i++) {
// System.out.println("hi");
c.increment();
}
};
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
t1.start();
t2.start();
// System.out.println(c.count); // it should be 2000
t1.join();
t2.join();// now with join it will work
System.out.println(c.count);// still you willl not get 2000, when executing multiple times you will get
// different results
}
}