Java 多线程之--ThreadLocal 简介

来源:互联网 发布:帝国时代2日本武士数据 编辑:程序博客网 时间:2024/05/16 14:44

<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">      在多线程开发中,经常会遇见在run方法里面调用一个公共的属性的事情,由于每次start都会创建一个线程,因此</span>
<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">所</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">有的线程共享一个属性,当其中任何一个线程更改了这个属性的值,这个属性在下面的使用过程中都会被改变,这</span>
<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">回导</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">致很多不期望发生的事情发生,这就叫做线程不安全的。先来一个例子说明这个问题。</span>
<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>

package com.bird.concursey;import java.util.Date;import java.util.concurrent.TimeUnit;public class UnsafeTask implements Runnable{private Date startDate;@Overridepublic void run() {startDate = new Date();System.out.println("start thread " + Thread.currentThread().getId() + " " + startDate);try {TimeUnit.SECONDS.sleep((int)Math.rint(Math.random() * 10));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Thread finish " + Thread.currentThread().getId() + " " + startDate);}public static void main(String[] args) {UnsafeTask task = new UnsafeTask();for(int i = 0; i < 10; i++) {Thread thread = new Thread(task);thread.start();try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}}}}


这里我们在主线程创建了十个分线程,然后每个线程都会实例化这个date,然后你会惊奇的发现,当所有的线程

运行完毕后,他们的结束时间都是一样的,运行结果如下。


start thread 8 Tue Sep 16 20:21:50 CST 2014start thread 9 Tue Sep 16 20:21:52 CST 2014start thread 10 Tue Sep 16 20:21:54 CST 2014start thread 11 Tue Sep 16 20:21:56 CST 2014start thread 12 Tue Sep 16 20:21:58 CST 2014Thread finish 10 Tue Sep 16 20:21:58 CST 2014Thread finish 11 Tue Sep 16 20:21:58 CST 2014Thread finish 8 Tue Sep 16 20:21:58 CST 2014start thread 13 Tue Sep 16 20:22:00 CST 2014Thread finish 9 Tue Sep 16 20:22:00 CST 2014start thread 14 Tue Sep 16 20:22:02 CST 2014start thread 15 Tue Sep 16 20:22:04 CST 2014Thread finish 14 Tue Sep 16 20:22:04 CST 2014start thread 16 Tue Sep 16 20:22:06 CST 2014Thread finish 12 Tue Sep 16 20:22:06 CST 2014start thread 17 Tue Sep 16 20:22:08 CST 2014Thread finish 16 Tue Sep 16 20:22:08 CST 2014Thread finish 13 Tue Sep 16 20:22:08 CST 2014Thread finish 15 Tue Sep 16 20:22:08 CST 2014Thread finish 17 Tue Sep 16 20:22:08 CST 2014


主要因为所有的线程公用一个属性,有一个线程改变了他的值,导致后面的其实调用的都是一个变量的值,我们

的期望是每个Thread都有自己的属性的值,大家都是独立的,那该怎么做呢。


package com.bird.concursey;import java.util.Date;import java.util.concurrent.TimeUnit;public class SafeTask implements Runnable {private static ThreadLocal<Date> startDate = new ThreadLocal<Date>() {protected Date initialValue() {return new Date();};};@Overridepublic void run() {System.out.println("start thread " + Thread.currentThread().getId()+ " " + startDate.get());try {TimeUnit.SECONDS.sleep((int) Math.rint(Math.random() * 10));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Thread finish " + Thread.currentThread().getId()+ " " + startDate.get());}public static void main(String[] args) {SafeTask task = new SafeTask();for (int i = 0; i < 10; i++) {Thread thread = new Thread(task);thread.start();try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}}}}


这就是正确的代码,主要就是加入了ThreadLocal这个类,主要的目的就是这样就能保证每次线程开启的时候都

会去调用他的initvalue方法给这个属性付一个初值,然后每个线程都会自己维护这个值,各个线程之间都是独立的,

我们通过get方法就能获得对应的值,当然他也提供了set方法来更改他的值,或者remove等等,反正现在就是线程安

全的了。


1 0
原创粉丝点击