《java多线程编程核心技术》----ThreadLocal
Posted windy13
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《java多线程编程核心技术》----ThreadLocal相关的知识,希望对你有一定的参考价值。
public class Run // 类ThreadLocal解决的是变量在不同线程间的隔离性,也就是 不同的线程拥有自己的值, // 不同线程中的值是可以放入ThreadLocal中进行保存的。 public static ThreadLocal t1= new ThreadLocal(); public static void main(String[] args) if(t1.get()==null) System.out.println("从未放过值"); t1.set("我的值"); System.out.println(t1.get()); System.out.println(t1.get()); //从未放过值 //我的值 //我的值
// 这里证明对应同一个threadlocal对象,不同的线程使用,线程之间是隔离的 // 线程a和线程b,同时修改threadlocal,不相互影响 public class Run // 验证线程变量的隔离性 public static ThreadLocal t1 = new ThreadLocal(); public static void main(String[] args) ThreadA a = new ThreadA(); ThreadB b = new ThreadB(); a.start(); b.start(); try for (int i = 0; i < 10; i++) t1.set("main" + (i + 1)); System.out.println("main get value = " + t1.get()); Thread.sleep(200); catch (InterruptedException e) e.printStackTrace(); static class ThreadA extends Thread @Override public void run() try for (int i = 0; i < 10; i++) t1.set("ThreadA" + (i + 1)); System.out.println("ThreadA get value = " + t1.get()); Thread.sleep(200); catch (InterruptedException e) e.printStackTrace(); static class ThreadB extends Thread @Override public void run() try for (int i = 0; i < 10; i++) t1.set("ThreadB" + (i + 1)); System.out.println("ThreadB get value = " + t1.get()); Thread.sleep(200); catch (InterruptedException e) e.printStackTrace(); main get value = main1 ThreadA get value = ThreadA1 ThreadB get value = ThreadB1 ThreadB get value = ThreadB2 ThreadA get value = ThreadA2 main get value = main2 main get value = main3 ThreadB get value = ThreadB3 ThreadA get value = ThreadA3 ThreadA get value = ThreadA4 ThreadB get value = ThreadB4 main get value = main4 ThreadA get value = ThreadA5 ThreadB get value = ThreadB5 main get value = main5 main get value = main6 ThreadB get value = ThreadB6 ThreadA get value = ThreadA6 main get value = main7 ThreadB get value = ThreadB7 ThreadA get value = ThreadA7 ThreadA get value = ThreadA8 ThreadB get value = ThreadB8 main get value = main8 ThreadA get value = ThreadA9 ThreadB get value = ThreadB9 main get value = main9 main get value = main10 ThreadA get value = ThreadA10 ThreadB get value = ThreadB10
// 这里说明,每个线程保存每个线程的单独的变量,且如果线程内部可以改变的threadlocal的值
// 我们可以在其中添加我们需要的类型,这里是string类型,a线程内部,每次都改变了threadlocal的值
public class Run
// 验证线程变量的隔离性 public static ThreadLocal t1 = new ThreadLocal(); public static void main(String[] args) ThreadA a = new ThreadA(); ThreadB b = new ThreadB(); a.start(); b.start(); static class ThreadA extends Thread @Override public void run() try for (int i = 0; i < 5; i++) t1.set("ThreadA" + (i + 1)); System.out.println("ThreadA 1 get value = " + t1.get()); System.out.println("ThreadA 2 get value = " + t1.get()); Thread.sleep(200); catch (InterruptedException e) e.printStackTrace(); static class ThreadB extends Thread @Override public void run() try for (int i = 0; i < 5; i++) t1.set("ThreadB" + (i + 1)); System.out.println("ThreadB get value = " + t1.get()); Thread.sleep(200); catch (InterruptedException e) e.printStackTrace(); ThreadA 1 get value = ThreadA1 ThreadA 2 get value = ThreadA1 ThreadB get value = ThreadB1 ThreadB get value = ThreadB2 ThreadA 1 get value = ThreadA2 ThreadA 2 get value = ThreadA2 ThreadB get value = ThreadB3 ThreadA 1 get value = ThreadA3 ThreadA 2 get value = ThreadA3 ThreadA 1 get value = ThreadA4 ThreadB get value = ThreadB4 ThreadA 2 get value = ThreadA4 ThreadA 1 get value = ThreadA5 ThreadB get value = ThreadB5 ThreadA 2 get value = ThreadA5
以上是关于《java多线程编程核心技术》----ThreadLocal的主要内容,如果未能解决你的问题,请参考以下文章