- package com.hzfuji.sdk.main;
- public class ThreadLocalTest {
- ThreadLocal<String> threadLocal = new ThreadLocal<String>();
- String str = "static";
- public static void main(String[] args) {
- ThreadLocalTest test = new ThreadLocalTest();
- test.test();
- }
- class Inner1 extends Thread {
- @Override
- public void run() {
- threadLocal.set("thread1");
- System.out.println("thread1 : " + threadLocal.get());
- System.out.println(str);
- }
- }
- class Inner2 extends Thread {
- @Override
- public void run() {
- // threadLocal.set("thread2");
- System.out.println("thread2 : " + threadLocal.get());
- System.out.println(str);
- }
- }
- public void test() {
- threadLocal.set("User");
- System.out.println(threadLocal.get());
- System.out.println(str);
- Inner1 inner1 = new Inner1();
- inner1.start();
- Inner2 inner2 = new Inner2();
- inner2.start();
- }
- }
对ThreadLocal的理解
最新推荐文章于 2023-08-27 21:33:30 发布