Как я могу заблокировать одну ветку и подождать, пока блокировка не будет выпущена в другой ветке?JAVA

Программисты JAVA общаются здесь
Anonymous
Как я могу заблокировать одну ветку и подождать, пока блокировка не будет выпущена в другой ветке?

Сообщение Anonymous »

Я просто хочу ждать в главном потоке до какого -то события в запуске.public class LockingTest {
private Lock initLock = new ReentrantLock();

@Test
public void waiting(){
initLock.lock();
final Condition condition = initLock.newCondition();
long t1= System.currentTimeMillis();
Thread th = new Thread(new Runnable(){
@Override public void run() {

try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {}
initLock.unlock();
}
});
th.start();

try {
condition.await(3000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {}

long t2= System.currentTimeMillis();
System.out.println(t2-t1);
}
}


Подробнее здесь: https://stackoverflow.com/questions/753 ... nother-thr

Вернуться в «JAVA»