ThreadLocal, InheritableThreadLocal, Transmittable ThreadLocal, 스 레 드 변수 사본
87802 단어 자바
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.2.0</version>
</dependency>
package com.jmdf.world;
import com.alibaba.ttl.TransmittableThreadLocal;
public class test01 {
private static ThreadLocal a =new ThreadLocal();
private static ThreadLocal b =new InheritableThreadLocal();
private static ThreadLocal d = new TransmittableThreadLocal();
private static int c = 0;
public static void main(String[] args) {
new Thread(() -> {
a.set(1);
b.set(1);
c = 1;
d.set(1);
new Thread(() -> {
System.out.println(" :1, ");
System.out.println("ThreadLocal------"+a.get());
System.out.println("InheritableThreadLocal-------"+b.get());
System.out.println(" -------------------"+c);
System.out.println("TransmittableThreadLocal-------"+d.get());
}).start();
}).start();
}
}
:1,
ThreadLocal------null
InheritableThreadLocal-------1
-------------------1
TransmittableThreadLocal-------1
PublicTransmittableThreadLocal
package com.jmdf.world;
import com.alibaba.ttl.TransmittableThreadLocal;
import static java.lang.Thread.sleep;
public class PublicTransmittableThreadLocal {
private static ThreadLocal tl = new TransmittableThreadLocal<>();
public static void main(String[] args) {
new Thread(() -> {
String mainThreadName = "main_01";
tl.set(1);
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (1), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (1), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (1), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
sleep(1L); // tl.set
tl.set(2); // ,
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (2), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (2), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (2), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
System.out.println(String.format(" -%s, =%s", Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
String mainThreadName = "main_02";
tl.set(3);
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (3), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (3), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (3), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
sleep(1L); // tl.set
tl.set(4); // ,
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (4), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (4), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
sleep(1L);
System.out.println(String.format(" (4), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
}).start();
System.out.println(String.format(" -%s, =%s", Thread.currentThread().getName(), tl.get()));
}).start();
}
private static void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
(1), -main_01, -Thread-3, =1
(2), -main_01, -Thread-9, =2
(3), -main_02, -Thread-6, =3
(3), -main_02, -Thread-5, =3
-Thread-1, =4
(4), -main_02, -Thread-11, =4
(2), -main_01, -Thread-12, =2
(2), -main_01, -Thread-8, =2
-Thread-0, =2
(4), -main_02, -Thread-13, =4
(1), -main_01, -Thread-4, =1
(3), -main_02, -Thread-2, =3
(4), -main_02, -Thread-10, =4
(1), -main_01, -Thread-7, =1
TestTransmittableThreadLocal
package com.jmdf.world;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestTransmittableThreadLocal {
// , TTL , , TTL ( )
private static ExecutorService executorService = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(2));
private static ThreadLocal tl = new TransmittableThreadLocal<>(); // TTL
public static void main(String[] args) {
new Thread(() -> {
String mainThreadName = "main_01";
tl.set(1);
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (1), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (1), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (1), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
sleep(1L); // tl.set
tl.set(2); // ,
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (2), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (2), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (2), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
System.out.println(String.format(" -%s, =%s", Thread.currentThread().getName(), tl.get()));
}).start();
new Thread(() -> {
String mainThreadName = "main_02";
tl.set(3);
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (3), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (3), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (3), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
sleep(1L); // tl.set
tl.set(4); // ,
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (4), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (4), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
executorService.execute(() -> {
sleep(1L);
System.out.println(String.format(" (4), -%s, -%s, =%s", mainThreadName, Thread.currentThread().getName(), tl.get()));
});
System.out.println(String.format(" -%s, =%s", Thread.currentThread().getName(), tl.get()));
}).start();
}
private static void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
(1), -main_01, -pool-1-thread-1, =1
(3), -main_02, -pool-1-thread-2, =3
-Thread-1, =4
-Thread-0, =2
(3), -main_02, -pool-1-thread-1, =3
(1), -main_01, -pool-1-thread-2, =1
(1), -main_01, -pool-1-thread-2, =1
(3), -main_02, -pool-1-thread-1, =3
(4), -main_02, -pool-1-thread-1, =4
(2), -main_01, -pool-1-thread-2, =2
(2), -main_01, -pool-1-thread-2, =2
(4), -main_02, -pool-1-thread-1, =4
(4), -main_02, -pool-1-thread-2, =4
(2), -main_01, -pool-1-thread-1, =2
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.