Repository navigation
Expand file tree
/
Copy pathThreads.java
More file actions
72 lines (57 loc) · 2.75 KB
/
Copy pathThreads.java
File metadata and controls
72 lines (57 loc) · 2.75 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package java_technics.interview;
import java.util.*;
/**
* https://wuciawe.github.io/jvm/2017/02/13/notes-on-concurrency-in-jvm.html
* https://habr.com/ru/company/otus/blog/353414/
* https://www.youtube.com/watch?v=CgRJUqO-dMQ
* https://www.youtube.com/watch?v=arGcok_I_DY&list=WL&index=7
* https://www.youtube.com/watch?v=mf4lC6TpclM
* https://www.youtube.com/watch?v=noDnSV7NCtw
* https://www.youtube.com/watch?v=Ky1_5mabd18
*/
public class Threads {
public static void main(String[] args) {
// потоки
ThreadLocal<String> threadLocal = new ThreadLocal<>();
InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
Thread thread = new Thread(() -> {
threadLocal.set("Aaaaaa"); // поток thread заюзал threadLocal
inheritableThreadLocal.set("Bbbbbbb"); // поток thread заюзал inheritableThreadLocal
System.out.println("\nMajor thread");
System.out.println(threadLocal.get()); // доступно, выводит значение
System.out.println(inheritableThreadLocal.get()); // доступно, выводит значение
Thread threadInner = new Thread(() -> { // вложенный поток
System.out.println("\nChild thread");
System.out.println(threadLocal.get()); // НЕдоступно, выводит null
System.out.println(inheritableThreadLocal.get()); // доступно, выводит значение
});
threadInner.start();
try {
threadInner.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
}
Thread thread1 = new Thread(() -> { // новый поток на одном уровне с потоком thread
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("\nAnother major thread");
System.out.println(threadLocal.get()); // НЕдоступно, выводит null (уже было занято потоком thread)
System.out.println(inheritableThreadLocal.get()); // НЕдоступно, выводит null (уже было занято потоком thread)
});
thread1.start();
try {
thread1.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}