-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadTest.java
More file actions
47 lines (32 loc) · 912 Bytes
/
Copy pathThreadTest.java
File metadata and controls
47 lines (32 loc) · 912 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
package Threads;
public class ThreadTest {
static int counter = 0;
public static void main(String[] args) throws InterruptedException {
MyThread t1 = new MyThread();
t1.start();
Thread x = new Thread(() -> {
for (int i = 0; i < 1000000; i++) {
counter++;
}
});
Thread y = new Thread(() -> {
for (int i = 0; i < 1000000; i++) {
counter++;
}
});
x.start();
Thread.sleep(2);
y.start();
x.join();
y.join();
System.out.println("Counter: " + counter);
}
// ThreadTest obj = new ThreadTest();
// Thread a = new Thread(obj::increment);
// Thread b = new Thread(obj::increment);
// a.start();
// b.start();
// a.join();
// b.join();
// System.out.println("Counter: " + counter);
}