threadLocal的使用
1.threadLocal的定义:
2.测试代码
class Test{ private ThreadLocal<String> str = new ThreadLocal<String>(); private List<String> list = new ArrayList<String>(); class A extends Thread { public void run() { str.set("zhangsan"); System.out.println("A...." + str.get()); list.add("xxx"); System.out.println("A<<<"+list.get(0)); }} class B extends Thread { public void run() { System.out.println("B...." + str.get()); list.add("xxx"); System.out.println("B<<<"+list.get(0)); }}}
Test2 t=new Test2(); Test2.A a=t.new A(); Test2.B b=t.new B(); a.start(); b.start();