-
Semaphore:
建構時一定要有一個整數參數如下 new Semaphore(1)設定某數值當此數值為零時其他的執行序會進入wait,其他說明如下可設定某個數值如:Semaphore(2)
建構時可給一數值CountDownLatch(int count),不可小於0在跑執行序時使用acquire()如能成功數值會扣1,當扣到為零時在呼叫acquire會產生wait的效果
呼叫release()可將數值加回去!
booleantryAcquire()Acquires a permit from this semaphore, only if one is available at the time of invocation.booleantryAcquire(int permits)Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.booleantryAcquire(int permits, long timeout, TimeUnit unit)Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been interrupted.booleantryAcquire(long timeout, TimeUnit unit)Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.CountDownLatch :
- 小於0會拋出
Exception in thread "main" java.lang.IllegalArgumentException: count < 0當數值歸零時可喚醒被wait的執行序!
使用countDown()減1
await()設定等待的執行序voidawait()
使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断。booleanawait(long timeout, TimeUnit unit)
使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断或超出了指定的等待时间。voidcountDown()
Reduces the number of counts by one in this CountDownLatch object. If the
count reaches zero, all the (a)waiting threads are released. If the current count
is already zero, nothing happens.longgetCount()
返回当前计数。StringtoString()
返回标识此锁存器及其状态的字符串。CyclicBarrier :
當執行序等待的數量到達設定時會執行某個Thread
建構值必須有參數
1. CyclicBarrier(int parties)这里的parties也是一个计数器,例如,初始化时parties里的计数是3,于是拥有该CyclicBarrier对象的线程当parties的计数为3时就唤醒,注:这里parties里的计数在运行时当调用CyclicBarrier:await()时,计数就加1,一直加到初始的值2. CyclicBarrier(int parties, Runnable barrierAction)當執行序到等待數量到設定時會呼叫barrierAction3 初始化不可為0或負數會產生IllegalArgumentException - 4 reset()重製到初始狀態。如果有任何線程在等待
- 這Barrier,他們會拋出例外BrokenBarrierException。
- 5 呼叫.await(1,TimeUnit.SECONDS);記得要tyr catch TimeoutException
- 6 當.await(1,TimeUnit.SECONDS);過時之後會拋出TimeoutException與BrokenBarrierException
Exchanger :
可在2個Thread中交換資料!
class Talk1 extends Thread{
Exchanger<String> _exhanger;
public Talk1(Exchanger<String> exhanger){
_exhanger = exhanger;
this.start();
}
public void run(){
try {
String value = _exhanger.exchange("Hi..... Im JOJO");
System.out.println("JoJo value:"+value);
String value2 = _exhanger.exchange("Hi..... how are you? Howard");
System.out.println("JoJo value:"+value2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Talk2 extends Thread{
Exchanger<String> _exhanger;
public Talk2(Exchanger<String> exhanger){
_exhanger = exhanger;
this.start();
}
public void run(){
try {
TimeUnit.SECONDS.sleep(2);
String value = _exhanger.exchange("Hi..... Im Howard");
System.out.println("HOWARD value:"+value);
TimeUnit.SECONDS.sleep(2);
String value2 = _exhanger.exchange("Is good...");
System.out.println("HOWARD value:"+value2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
HOWARD value:Hi..... Im JOJO
JoJo value:Hi..... Im Howard
JoJo value:Is good...
HOWARD value:Hi..... how are you? Howard
HOWARD value:Hi..... Im JOJO
JoJo value:Hi..... Im Howard
JoJo value:Is good...
HOWARD value:Hi..... how are you? Howard
Phaser :
會設定一個數值,等到完成數量等於設定時會解開等待!phaser.arriveAndAwaitAdvance();//會增加完成數量並等待
arriveAndDeregister//會移除註冊減少設定值
- awaitAdvance會將執行緒wait直到指定的phaser到達目標
- bulkRegister(int parties)大量註冊
- 可複寫
onAdvance(int phase, int numParties)達到時最後執行的方法,retun true - isTerminated就回傳true return false 就回傳False;
phase--幾個階段 numParties--多少執行緒- 此方法回傳值會影響isTerminated
- phase(階段)由0開始
- awaitAdvance(int phase) 在指定的階段等待
- getArrivedParties() 取得已達到party的數量
- getUnarrivedParties() 取得未已達到party的數量
沒有留言:
張貼留言