首页 > 其他 > 详细

练习题之ThreadLocal

时间:2016-04-06 21:46:39      阅读:268      评论:0      收藏:0      [点我收藏+]
public class  ThreadLocalMain {
 
  private static ThreadLocal<Integer> value = new ThreadLocal<Integer>() {
     @Override
     protected Integer initialValue(){
        return 0;
     }
  };
 
 class TestThread implements Runnable {
   private int index;
   public TestThread(int index) {
      this.index = index;
   }
  
  public void run() {
    System.out.println("线程“+index +"的初始Value:" + value.get());
    for(int i=0;i<10;i++) {
        value.set(value.get()+i);
     }
    System.out.println("线程“+index +"的累加Value:" + value.get());
  }
  
  public static void main(String [] args) {
    ThreadLocalMain main = new ThreadLocalMain();
    for(int i=0;i<5;i++) {
      TestThread testThread = main.new TestThread(i);
      new Thread(testThread).start();
    }
  }
 }  
}

关于ThreadLocal的使用请参见:http://ifeve.com/java-theadlocal/

练习题之ThreadLocal

原文:http://www.cnblogs.com/moonandstar08/p/5361186.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!