ThreadLocal详解

来源:互联网 发布:易言软件下载 编辑:程序博客网 时间:2024/06/16 14:48

http://blog.csdn.net/u011860731/article/details/48733073

http://www.cnblogs.com/jym-sunshine/p/4694653.html


package com.yano.reflect;public class ThreadLocalExample {public static class MyRunnable implements Runnable{private ThreadLocal threadLocal = new ThreadLocal();@Overridepublic void run() {threadLocal.set((Math.random()*100D));try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(threadLocal.get());}}public static void main(String[] args){MyRunnable myRunnable  = new MyRunnable();Thread thread1 = new Thread(myRunnable);Thread thread2 = new Thread(myRunnable);thread1.start();thread2.start();}}