什么是ThreadLocal:
概念:ThreadLocal是Thread的局部变量,用于编多线程程序,对解决多线程程序的并发问题有一定的启示作用。
ThreadLocal使用实战:
为共享变量在每个线程中创建一个副本,每个线程可以访问自己内部的副本变量
作用:每个线程之间相互不影响保证线程的一个安全性:
代码示例:
package com.thread;
public class ThreadLocalDemo {
private static Integer num = 0;
private static ThreadLocal<Integer> numLocal =
new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 0;
}
};
public static void main(String[] args) {
Thread[] threads = new Thread[5];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(()->{
// num += 5;
int x = numLocal.get().intValue();
x+=5;
numLocal.set(x);
System.out.println(Thread.currentThread().getName()+ " : " + numLocal.get());
},"Thread " + i);
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
}
}