当前位置: 首页>后端>正文

线程安全:ThreadLocal

线程安全:ThreadLocal,第1张

public class ThreadLocaDemo {

????private static ThreadLocal<String> localVar = new ThreadLocal<String>();

????static void print(String str) {

//打印当前线程中本地内存中本地变量的值

????????System.out.println(str + " :" + localVar.get());

//清除本地内存中的本地变量

????????localVar.remove();

????}

????public static void main(String[] args) throws InterruptedException {

????????new Thread(new Runnable() {

????????????public void run() {

????????????????ThreadLocaDemo.localVar.set("local_A");

????????????????print("A");

//打印本地变量

????????????????System.out.println("after remove : " + localVar.get());

????????????}

????????},"A").start();

????????Thread.sleep(1000);

????????new Thread(new Runnable() {

????????????public void run() {

????????????????ThreadLocaDemo.localVar.set("local_B");

????????????????print("B");

????????????????System.out.println("after remove : " + localVar.get());

????????????}

????????},"B").start();

????}

}


https://www.xamrdz.com/backend/3eb1924155.html

相关文章: