---
title: "ThreadLocal and synchronized 补充"
author: "dovecat"
date: "2007-05-25T13:05:11Z"
tags: []
categories: ["企业架构"]
slug: "threadlocal-and-synchronized"
draft: false
lastmod: "2007-05-25T13:05:11Z"
id: 82984
url: "https://dovecat.iteye.com/blog/82984"
---

<http://www.iteye.com/topic/81936?page=1>
以上是原贴.
本文只是针对原贴的补充.

对于ThreadLocal和synchronized的区别,请看下面的例子估计大家更能清楚认识.希望我能在kyluan原贴的基础上把这个区别说清楚.

btw:<span style="color:red">这个例子是一个使用ThreadLocal不当的例子,请不要在项目中如此使用</span>.


```

public class TestThreadLocal {
  public static void main(String[] args) throws Exception {
    ThreadLocal myThreadLocal = new ThreadLocal();
    StaffInfoVO staff1 = new StaffInfoVO();
    staff1.setName("default");
    staff1.setCount(new Integer(1));
    for (int i = 0; i < 10; i++) {
      MyThread myt = new MyThread(staff1, myThreadLocal);
      Thread t1 = new Thread(myt);
      t1.setName("myThread" + i);
      t1.start();
    }
  }

  public static class MyThread implements Runnable {

    private StaffInfoVO staff1;

    private ThreadLocal myThreadLocal;

    public MyThread(StaffInfoVO staff, ThreadLocal myThreadLocal) {
      staff1 = staff;
      this.myThreadLocal = myThreadLocal;
      System.out.println("con staff" + staff);
    }

    public void run() {
      myThreadLocal.set(staff1);
      while (true) {
        StaffInfoVO staff = (StaffInfoVO) myThreadLocal.get();
        // System.out.println("staff:" + staff);
        int i = staff.getCount().intValue();
        System.out.println("Thread name:" + Thread.currentThread().getName() + " staff count:" + i);

        staff.setCount(new Integer(i + 1));

        try {
          Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}
public class StaffInfoVO implements Serializable {

  /**
   * 
   */
  private static final long serialVersionUID = -57676961756664705L;

  private String name;

  private String staffNo;

  private List roles;

  private Integer count;

  // 如果不使用synchronized,大家一眼就看到区别了.
  public synchronized Integer getCount() {
    return count;
  }
  
  // 如果不使用synchronized,大家一眼就看到区别了.
  public synchronized void setCount(Integer count) {
    this.count = count;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List getRoles() {
    return roles;
  }

  public void setRoles(List roles) {
    this.roles = roles;
  }

  public String getStaffNo() {
    return staffNo;
  }

  public void setStaffNo(String staffNo) {
    this.staffNo = staffNo;
  }

}

```


首先产生一个<span style="color:red">引用</span>对象staff1 .
然后产生10个MyThread 类型的线程对象.
这10个线程中每一个都持有私有的ThreadLocal对象.
在这10个线程开始start的时候,在run方法中将staff1对象设置到各自私有的ThreadLocal对象中.

当我们注释掉staff1中的synchronized时候,我们马上就看到出现了count数据的不一致.
只有当我们使用synchronized,不一致的情况解决了.
当StaffInfoVO的count的set/get方法不是synchronized的时候,虽然每个Thread维护了一个对应的ThreadLocal,而ThreadLocal只是对于Thread对象中的ThreadLocalMap的接口暴露(get/set添加和取得保存的对象)对于被保存的对象,只是保存了引用的情况下,并且被保存对象的状态改变的方法不是synchronized的时候,并不能保证同步. 这个是ThreadLocal的局限性.并没有synchronized关键字那么安全.在我看来ThreadLocal MS不是用来处理同步,而只是为了保存当前线程自有的某个状态.当ThreadLocal.set(Object obj)方法时,取得Thread.currentThread.ThreadLocalMap对象,然后将自己作为KEY保存到ThreadLocalMap中.ThreadLocal.get处理方法类似.

所以最后我得出的结论就是:synchronized是用来处理多线程环境下的数据同步,而<span style="color:red">ThreadLocal只是为了保存当前线程私有的某种状态</span>.

以上内容,如有不当,请指出.
谢谢.