[問題] 想請問lock的用法
想做到類似 BlockingQueue 的功能
請問該怎麼做?
例如
void start(Item t){
//解除下面的get()的 block
}
void run(){
while(true){
//block 這條 thread
dosomething();
}
}
感覺起來要用lock,但是又怕start的時候被 block住
我希望只有在run()被block的狀況下才解除block
其他狀況什麼都不做
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.87.115
※ 文章網址: https://www.ptt.cc/bbs/java/M.1432871603.A.022.html
→
05/29 14:02, , 1F
05/29 14:02, 1F
→
05/29 15:03, , 2F
05/29 15:03, 2F
private class DrawRunnable implements Runnable{
void update(){
last=System.currentTimeMillis();
synchronized (this){
notifyAll();
}
}
public void run() {
while (run) {
try {
//dosomething
synchronized (this) {
wait(); // <= 卡在這裡,其他執行序叫了update也沒醒
}
Log.d("F23ko", "DrawRunnable STEP UNLOCK");
} catch (InterruptedException e) {
}
}
}
}
※ 編輯: LaPass (125.227.87.115), 05/29/2015 15:10:55
→
05/29 15:15, , 3F
05/29 15:15, 3F
→
05/29 15:22, , 4F
05/29 15:22, 4F
→
05/29 15:22, , 5F
05/29 15:22, 5F
→
05/29 15:26, , 6F
05/29 15:26, 6F
推
05/29 17:25, , 7F
05/29 17:25, 7F
推
05/29 17:37, , 8F
05/29 17:37, 8F
→
05/29 17:37, , 9F
05/29 17:37, 9F
→
05/29 18:18, , 10F
05/29 18:18, 10F
→
05/29 19:22, , 11F
05/29 19:22, 11F
java 近期熱門文章
PTT數位生活區 即時熱門文章