執行緒學習(2)

2021-08-29 01:47:40 字數 4299 閱讀 2305

參考知乎關於lockinterruptibly用法

getholdcount() 方法:查詢當前執行緒保持此鎖定的個數,也就是呼叫 lock() 的次數;請注意這是當前執行緒鎖定的次數

getqueuelength() 方法:返回正等待獲取此鎖定的執行緒估計數目;

isfair() 方法:判斷是不是公平鎖;

lockinterruptibly()

此方法比較特別,指的深入**一下,通過網上查閱資料,首先研究一下執行緒的中斷機制

lockinterruptibly的方法原理是:

下面是原作者的例子:

public class testlock 

}, "child thread -1");

t1.start();

thread.sleep(1000);

t1.interrupt();

thread.sleep(4000);

system.out.println(1);

lock.unlock();

}public static void main(string args) throws exception

}執行結果:

1child thread -1 true

child thread -1 interrupted.

可以看出lock方法對於子執行緒在等待鎖過程中的中斷,沒有採取任何措施,只是將子執行緒的中斷位設定了

public class testlockinterruptibly catch (interruptedexception e)

}}, "child thread -1");

t1.start();

thread.sleep(1000);

t1.interrupt();

thread.sleep(1000000);

} public static void main(string args) throws exception

}結果:

child thread -1 interrupted.

這種情況對應執行緒已經被中斷,然後又lockinterruptibly,會丟擲中斷異常

public class testlockinterruptibly catch (interruptedexception e)

}}, "child thread -1");

t1.start();

t1.interrupt();

thread.sleep(1000);

t1.interrupt();

thread.sleep(1000000);

} public static void main(string args) throws exception

}執行結果:

child thread -1 interrupted.

reentrantreadwritelock 有兩個鎖:乙個是與讀相關的鎖,稱為「共享鎖」;另乙個是與寫相關的鎖,稱為「排它鎖」,也就是多個讀鎖之間不互斥,讀鎖與寫鎖互斥,寫鎖與寫鎖互斥。

在沒有執行緒進行寫操作時,進行讀操作的多個執行緒都可以獲取到讀鎖,而寫操作的執行緒只有獲取寫鎖後才能進行寫入操作。即:多個執行緒可以同時進行讀操作,但是同一時刻只允許乙個執行緒進行寫操作。

讀讀共享;

寫寫互斥;

讀寫互斥;

寫讀互斥;

寫寫共享的例子:

public class reentrantreadwritelockdemo

private void read()

} finally

} catch (interruptedexception e) }}

即寫鎖的獲取是序列的

讀讀的例子:

public class reentrantreadwritelockdemo

private void read()

} finally

} catch (interruptedexception e) }}

可以看出a/b交替執行!

countdownlatch是乙個多執行緒控制工具類,它允許乙個或多個執行緒一直等待,直到其他執行緒的操作執行完後再執行。

比如下面這個例子,車子等人,當七個人到齊後,開車

public class countdownlatchdemo catch (interruptedexception e)

//每上車乙個人,需要等待的數減1

countdownlatch.countdown();

}).start();

}//等待檢查,即上述7個執行緒執行完畢之後,執行await後邊的**

countdownlatch.await();

system.out.println("七個人到齊,開車");}}

一般使用的時候,在主線程啟動啟動子執行緒後,會執行countdownlatch.await();阻塞主線程,讓子執行緒得到機會執行,

每一次子執行緒執行完畢後,執行 countdownlatch.countdown();通知主線程,當countdown到0後,主線程開始執行 countdownlatch.await();後面的**

cyclicbarrier 和 countdownlatch 都是類似功能,但是cyclicbarrier 會阻塞子程序,而countdownlatch會阻塞主程序,而且cyclicbarrier可以重置操作,countdownlatch則是一次性的

cyclicbarrier 的字面意思是可迴圈使用(cyclic)的屏障(barrier)。它要做的事情是,讓一組執行緒到達乙個屏障(也可以叫同步點)時被阻塞,直到最後乙個執行緒到達屏障時,屏障才會開門,所有被屏障攔截的執行緒才會繼續幹活。

cyclicbarrier 預設的構造方法是 cyclicbarrier(int parties),其引數表示屏障攔截的執行緒數量,每個執行緒呼叫 await 方法告訴 cyclicbarrier 我已經到達了屏障,然後當前執行緒被阻塞。

cyclicbarrier 強調的是 n 個執行緒,大家相互等待,只要有乙個沒完成,所有人都得等著。

下面是從網上部落格摘抄的例子,召喚神龍,需要7個法師去尋找龍珠,但這7個法師並不是一下子就能號召起來的,所以要等待召集齊7個法師,然後在秋名山頂燒香拜佛為這7位法師送行,讓他們同時出發,前往不同的地方尋找龍珠(敲黑板:這是第乙個屏障點),在這七位法師臨行時約定找到龍珠之後,還回到這個地方等待其他法師找到龍珠之後一起去見我。幾年之後,第乙個法師回來了,然後等待其他的法師……最後所有的法師全部到齊(敲黑板:這是第乙個屏障點),然後組隊來找我召喚神龍。

public class summondragondemo 

});//召集齊7位法師

for (int i = 1; i <= thread_count_num; i++) catch (interruptedexception | brokenbarrierexception e)

}).start();}}

/*** 召喚神龍:1、收集龍珠;2、召喚神龍

*/private static void summondragon()

});//收集7顆龍珠

for (int i = 1; i <= thread_count_num; i++) catch (interruptedexception | brokenbarrierexception e)

}).start();}}

}執行結果:

召集第1個法師

召集第3個法師

召集第2個法師

召集第4個法師

召集第5個法師

召集第6個法師

召集第7個法師

7個法師召集完畢,同時出發,去往不同地方尋找龍珠!

第1顆龍珠已收集到!

第2顆龍珠已收集到!

第3顆龍珠已收集到!

第4顆龍珠已收集到!

第5顆龍珠已收集到!

第6顆龍珠已收集到!

第7顆龍珠已收集到!

集齊七顆龍珠!召喚神龍!

new cyclicbarrier(int num, runnable runable) 意思是建立乙個記憶體屏障,只有num個子執行緒都到達屏障處,則有最後乙個到達的執行緒執行runable裡的動作

通過下列圖,可以看到其實是thread 6方法在執行召喚神龍的任務

執行緒學習筆記2

在兩個程序間進行socket通訊中,在其中乙個程序中建立新的執行緒,用於監聽,程式設計中發現自己對執行緒的操作僅限於執行緒的建立,銷毀,以及互斥量的操作,感覺還有許多關於執行緒的東西沒有掌握,so,需要好好學習總結一下,不足之處望指出 為什麼要引入執行緒呢?雖然程序可以提高cpu的利用率,但是程序之...

C 多執行緒學習2

include stdafx.h include beginthread.h ifdef debug define new debug new undef this file static char this file file endif include stdafx.h include wind...

java學習之執行緒2

賣演唱會票例子 保證 多個執行緒訪問共享的資料 資料只有乙份 public static void main string args 介面實現執行緒的方式 class ticketrunnable implements runnable catch interruptedexception e 賣s...