标签:int stat over exce 定义 自己 log xtend 存储
变量值得共享可以使用public static变量的形式,所有的线程都使用同一个public static变量。如果想实现每一个线程都有自己的共享变量该如何解决呢?JDK中提供的ThreadLocal正是解决这样的问题。
ThreadLocal主要解决的就是每个线程绑定自己的值,可以将ThreadLocal类比喻成全局存放数据的盒子,盒子中可以存储每个线程的私有数据。ThreadLocal解决的是变量在不同线程键的隔离性,也就是不同 线程拥有自己的值,不同线程中的值是可以放入ThreadLocal类中进行保存的。
package org.github.lujiango; public class Test03 { public static ThreadLocal<Object> tl = new ThreadLocal<Object>(); static class ThreadA extends Thread { @Override public void run() { try { tl.set("ThreadA"); Thread.sleep(200); System.out.println("ThreadA get value = " + tl.get()); } catch (Exception e) { e.printStackTrace(); } } } static class ThreadB extends Thread { @Override public void run() { try { tl.set("ThreadB"); Thread.sleep(200); System.out.println("ThreadB get value = " + tl.get()); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { try { ThreadA a = new ThreadA(); ThreadB b = new ThreadB(); a.start(); b.start(); } catch (Exception e) { e.printStackTrace(); } } }
第一次调用ThreadLocal类的get()方法返回值是null,可以自定义protected T initialValue()方法,设置默认值。
package org.github.lujiango; class ThreadLocalExt extends ThreadLocal<String> { @Override protected String initialValue() { return "defalut"; } } public class Test03 { public static ThreadLocalExt tl = new ThreadLocalExt(); static class ThreadA extends Thread { @Override public void run() { try { System.out.println("ThreadA get value = " + tl.get()); tl.set("ThreadA"); Thread.sleep(200); System.out.println("ThreadA get value = " + tl.get()); } catch (Exception e) { e.printStackTrace(); } } } static class ThreadB extends Thread { @Override public void run() { try { System.out.println("ThreadB get value = " + tl.get()); tl.set("ThreadB"); Thread.sleep(200); System.out.println("ThreadB get value = " + tl.get()); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { try { ThreadA a = new ThreadA(); ThreadB b = new ThreadB(); a.start(); b.start(); } catch (Exception e) { e.printStackTrace(); } } }
标签:int stat over exce 定义 自己 log xtend 存储
原文地址:http://www.cnblogs.com/lujiango/p/7580790.html