-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedMethod.java
More file actions
37 lines (27 loc) · 916 Bytes
/
SynchronizedMethod.java
File metadata and controls
37 lines (27 loc) · 916 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
package Thread.Day_5;
class CounterInSynchronizedMethod {
int count = 0;
synchronized void increment() {
count++;
}
}
public class SynchronizedMethod {
public static void main(String[] args) throws InterruptedException {
CounterInSynchronizedMethod counterInSynchronizedMethod = new CounterInSynchronizedMethod();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 10000; i++) {
counterInSynchronizedMethod.increment();
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 10000; i++) {
counterInSynchronizedMethod.increment();
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("The final count in synchronized method: " + counterInSynchronizedMethod.count);
}
}