java併發程式設計一一多執行緒執行緒安全(二)
阿新 • • 發佈:2018-11-07
1.多執行緒死鎖
1.1什麼是多執行緒死鎖?
同步中巢狀同步,導致鎖無法釋放
程式碼示例:
class Thread009 implements Runnable {
private int trainCount = 100;
private Object oj = new Object();
public boolean flag = true;
public void run() {
if (flag) {
while (trainCount > 0) {
synchronized (oj) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
sale();
}
}
} else {
while (trainCount > 0 ) {
sale();
}
}
}
public synchronized void sale() {
synchronized (oj) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
if (trainCount > 0) {
System.out.println(Thread.currentThread().getName() + "," + "出售第" + (100 - trainCount + 1) + "票");
trainCount--;
}
}
}
}
public class Test009 {
public static void main(String[] args) throws InterruptedException {
Thread009 threadTrain = new Thread009();
Thread t1 = new Thread(threadTrain, "視窗1");
Thread t2 = new Thread(threadTrain, "視窗2");
t1.start();
Thread.sleep(40);
threadTrain.flag = false;
t2.start();
}
}
2.ThreadLocal
2.1什麼是ThreadLocal?
ThreadLocal提高一個執行緒的區域性變數,訪問某個執行緒擁有自己區域性變數。
當使用ThreadLocal維護變數時,ThreadLocal為每個使用該變數的執行緒提供獨立的變數副本。,
2.1.1ThreadLocal的介面方法
ThreadLocal類介面很簡單,只有4個方法。
1. void set(object values) 設定當前執行緒的執行緒區域性變數的值。
2. public Object get() 該方法返回當前執行緒鎖對應的執行緒區域性變數
3. public void remove() 將當前執行緒區域性標量的值刪除,目的是為了減少記憶體的佔用,該方法是
JDK5.0 新增的方法。需要指出的是當執行緒結束後,對應該執行緒的劇本變數將自動被垃圾回收,
所以顯示呼叫該方法清除執行緒的區域性變數並不是必須的操作,但它可以加快記憶體回收的速度。
4. protected Object initalValue() 返回該執行緒區域性變數的初始值,該方法是一個protected 的方法,顯然是為了
讓子類覆蓋而設計的,這個方法是一個延遲呼叫的方法,線上程第一次呼叫get()或 set(Object)時才執行。
並且僅執行一次。ThreadLocal中的預設實現直接返回一個null
程式碼示例:建立三個執行緒,每個執行緒生成自己獨立的序列號。
class Res {
// 生成序列號共享變數
public static Integer count = 0;
public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return 0;
};
};
public Integer getNum() {
int count = threadLocal.get() + 1;
threadLocal.set(count);
return count;
}
}
public class ThreadLocalTest extends Thread {
private Res res;
public ThreadLocalTest(Res res) {
this.res = res;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + "---" + "i---" + i + "--num:" + res.getNum());
}
}
public static void main(String[] args) {
Res res = new Res();
ThreadLocalTest threadLocaDemo1 = new ThreadLocalTest(res);
ThreadLocalTest ThreadLocalTest = new ThreadLocalTest(res);
ThreadLocalTest threadLocaDemo3 = new ThreadLocalTest(res);
threadLocaDemo1.start();
ThreadLocalTest.start();
threadLocaDemo3.start();
}
}
2.1.2ThreadLocal實現原理
ThreadLocal通過map集合
Map.put(“當前執行緒”,value)