-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrencyExample.java
More file actions
31 lines (28 loc) · 924 Bytes
/
Copy pathConcurrencyExample.java
File metadata and controls
31 lines (28 loc) · 924 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
public class ConcurrencyExample {
public static void main(String[] args) {
Runnable task1 = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("Task 1 - Count " + i);
try {
Thread.sleep(50); // Simulating work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable task2 = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("Task 2 - Count " + i);
try {
Thread.sleep(50); // Simulating work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
thread1.start();
thread2.start();
}
}