本文是學習網絡上的文章時的總結,感謝大家無私的分享。
1、如果創建一個類對象,實現Runnable接口,然後多個Thread對象使用同樣的Runnable對象,全部的線程都共享同樣的屬性。這意味着,如果你在一個線程裏改變一個屬性,全部的線程都會受到這個改變的影響。如果希望程序裏的哥哥線程的屬性不會被共享,Java併發API提供了一個很清楚的機制叫本地線程變量。
2、Java併發API包括Inheritable ThreadLocal類提供線程創建線程的值的遺傳性。如果線程A有一個本地線程變量,然後它創建了另一個線程B,那麼線程B將有與A相同的本地線程變量值。你可以覆蓋childValue方法來初始子線程的本地線程變量的值。它接收付線程的本地線程變量作爲參數。
下面先介紹一個不使用本地線程變量的代碼
package chapter;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class UnsafeTask implements Runnable{
private Date startDate;
@Override
public void run() {
startDate = new Date();
System.out.printf("Starting Thread :%s:%s\n", Thread.currentThread().getId(),startDate);
try {
TimeUnit.SECONDS.sleep((int)Math.rint(Math.random()*10));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("Thread Finished:%s:%s\n", Thread.currentThread().getId(),startDate);
}
}
package chapter;
import java.util.concurrent.TimeUnit;
public class Main9 {
/**
* <p>
* </p>
* @author zhangjunshuai
* @date 2014-8-22 下午4:30:20
* @param args
*/
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
}
使用本地線程變量
package chapter;
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();
}
};
@Override
public void run() {
System.out.printf("Starting Thread:%s:%s\n", Thread.currentThread().getId(),startDate.get()
);
try {
TimeUnit.SECONDS.sleep((int)Math.rint(Math.random()*10));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.printf("Thread Finished:%s:%s\n", Thread.currentThread().getId(),startDate.get());
}
}
package chapter;
import java.util.concurrent.TimeUnit;
public class Main9 {
/**
* <p>
* </p>
* @author zhangjunshuai
* @date 2014-8-22 下午4:30:20
* @param args
*/
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}