Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7e5e520

Browse filesBrowse files
committed
add FastThreadLocalTest
1 parent 283da8a commit 7e5e520
Copy full SHA for 7e5e520

File tree

Expand file treeCollapse file tree

2 files changed

+47
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+47
-8
lines changed
Open diff view settings
Collapse file

‎Spring-Netty/src/main/java/com/bruis/learnnetty/netty/fastthreadlocal/FastThreadLocalTest.java‎

Copy file name to clipboardExpand all lines: Spring-Netty/src/main/java/com/bruis/learnnetty/netty/fastthreadlocal/FastThreadLocalTest.java
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ public static void main(String[] args) {
4848
Object object0 = threadLocal0.get();
4949
System.out.println(Thread.currentThread().getName() + "---> " + object0);
5050

51-
while (true) {
52-
System.out.println(Thread.currentThread().getName() + "---> " + (threadLocal0.get() == object0));
53-
try {
54-
Thread.sleep(1000);
55-
} catch (Exception e) {
56-
e.printStackTrace();
57-
}
58-
}
51+
System.out.println(Thread.currentThread().getName() + "---> " + (threadLocal0.get() == object0));
52+
// while (true) {
53+
// System.out.println(Thread.currentThread().getName() + "---> " + (threadLocal0.get() == object0));
54+
// try {
55+
// Thread.sleep(1000);
56+
// } catch (Exception e) {
57+
// e.printStackTrace();
58+
// }
59+
// }
5960
}).start();
6061
}
6162
}
Collapse file
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.bruis.learnnetty.netty.fastthreadlocal;
2+
3+
/**
4+
* @author lhy
5+
* @date 2021/7/14
6+
*/
7+
public class ThreadLocalTest {
8+
9+
private static ThreadLocal<Object> threadLocal0 = new ThreadLocal<>();
10+
11+
private static ThreadLocal<Object> threadLocal1 = new ThreadLocal<>();
12+
13+
public static void main(String[] args) {
14+
// 线程外
15+
System.out.println("main线程1: " + threadLocal0.get());
16+
Object o = new Object();
17+
threadLocal0.set(o);
18+
19+
new Thread(() -> {
20+
Object threadObject = threadLocal0.get();
21+
System.out.println("线程内: " + threadObject);
22+
if (threadObject == null) {
23+
Object newObject = new Object();
24+
System.out.println("新new一个对象:" + newObject);
25+
threadLocal0.set(newObject);
26+
}
27+
try {
28+
Thread.sleep(1000);
29+
System.out.println("休眠了一秒");
30+
} catch (Exception e) {
31+
e.printStackTrace();
32+
}
33+
System.out.println("线程内,从ThreadLocal获取:" + threadLocal0.get());
34+
}).start();
35+
36+
System.out.println("main线程2: " + threadLocal0.get());
37+
}
38+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.