package test.synchornize;
import java.util.concurrent.locks.*;
class runab implements Runnable{
boolean x = true;
static Lock lock = new ReentrantLock();
static Condition con1 = lock.newCondition();
static Condition con2 = lock.newCondition();
runab(boolean x){
this.x = x;
}
public void run() {
lock.lock();
try {
System.out.println("Start---" + Thread.currentThread().getName());
if(x) {
x = false;
try {con1.await();con2.await();} catch (InterruptedException e) {}
}
System.out.println("End---" + Thread.currentThread().getName());
} finally {
con2.signalAll();
con1.signalAll();
lock.unlock();
}
}
}
public class showLock {
public static void main(String[] args) {
runab rab = new runab(true);
runab rab1 = new runab(false);
Thread th = new Thread(rab);
Thread th1 = new Thread(rab1);
th.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
th1.start();
}
}
请问finally里面怎样写才能,让线程th继续下去呢?(即让程序最后一行输出
“End---Thread-0”)
胡说叔叔
相关分类