JavaConcurrency 5 執行緒組

2021-09-11 02:22:54 字數 1486 閱讀 3741

執行緒組:

執行緒組表示乙個執行緒的集合,執行緒組還可以包含其他的執行緒組,執行緒組構成一棵樹,在樹中,除了初始執行緒組外,每個執行緒組都有乙個父執行緒組。允許執行緒訪問有關自己的執行緒組資訊,但是不允許訪問有關其執行緒組的父執行緒組or其他任何執行緒組的資訊。

threadgroup

(string name)

構造乙個新執行緒組。

threadgroup

(threadgroup parent, string name)

建立乙個新執行緒組。

thread

(threadgroup group, runnable target)

分配新的 thread 物件。

thread

(threadgroup group, runnable target, string name)

分配新的 thread 物件,以便將 target 作為其執行物件,將指定的 name 作為其名稱,並作為 group 所引用的執行緒組的一員。

thread

(threadgroup group, runnable target, string name,

long stacksize)

分配新的 thread 物件,以便將 target 作為其執行物件,將指定的 name 作為其名稱,作為 group 所引用的執行緒組的一員,並具有指定的堆疊大小。

thread

(threadgroup group, string name)

分配新的 thread 物件。

常用操作方法:

int

activecount

():獲取執行緒組中活動執行緒的數量

· interrupt

():中斷執行緒組中所有執行緒

· isdaemon

():是否為後台執行緒組

· setdaemon

(boolean daemon):設定為後台執行緒組

· setmaxpriority

(int pri):設定執行緒組的最高優先順序

呼叫demo:

public

class

fist

//提供指執行緒名,執行緒組的構造器

public

mythread

(threadgroup group,string name)

@override

public

void

run()}

public

static

void

main

(string args)

throws interruptedexception

}

執行緒是在創造的同時加入執行緒組中,然後start,activecount()可以獲得活動執行緒的總數,但由於執行緒是動態的,所以這個值是乙個預估值,list()可以列印這個執行緒組中所有的執行緒資訊。

執行緒示例 5

2010 10 2 執行緒同步 深入理解 幾個執行緒操作共同資源會出現併發問題 排隊上廁所 package com.thread public class demo5 class ticketwindow implements runnable catch interruptedexception ...

python 多執行緒5執行緒同步

互斥鎖是最簡單的執行緒同步機制,python提供的condition物件提供了對複雜執行緒同步問題的支援。condition被稱為條件變數,除了提供與lock類似的acquire和release方法外,還提供了wait和notify方法。執行緒首先acquire乙個條件變數,然後判斷一些條件。如果條...

5 建立多執行緒

到目前為止,我們僅用到兩個執行緒 主線程和乙個子執行緒。然而,你的程式可以建立所需的更多執行緒。例如,下面的程式建立了三個子執行緒 create multiple threads.class newthread implements runnable this is the entry point ...