-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadDebug.java
49 lines (46 loc) · 1.27 KB
/
ThreadDebug.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
45
46
47
48
49
/**
*
*/
public class ThreadDebug {
public static void main(String[] args) throws InterruptedException {
MyClass ii = new MyClass();
Thread t2 = null;
Thread t1 = new Thread(() -> {
for (int i = 0; i < 10000; i++) {
ii.inc();
System.out.println("i = " + i);
try {
//t2.wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.setName("Поток 1");
t2 = new Thread(() -> {
for (int i = 0; i < 10000; i++) {
ii.inc();
System.out.println("Thread2: i = " + i);
try {
t1.wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t2.setName("Поток 2");
t1.start();
t2.start();
t1.join();
t2.join();
}
static public class MyClass {
public int i;
synchronized void inc() {
i++;
System.out.println(Thread.currentThread().getName());
}
}
}