package projects.debug.learn;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest
{
public void CountDownLatchExa() throws InterruptedException
{
final CountDownLatch countDownBegin = new CountDownLatch(1);
final CountDownLatch countDownEnd = new CountDownLatch(2);
Thread threadA = new Thread()
{
@Override
public void run()
{
try
{
countDownBegin.await();
System.out.println("threadA");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
countDownEnd.countDown();
}
}
};
Thread threadB = new Thread()
{
@Override
public void run()
{
try
{
countDownBegin.await();
System.out.println("threadB");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
countDownEnd.countDown();
}
}
};
System.out.println("CountDownLatchExa-X");
threadA.start();
threadB.start();
countDownBegin.countDown();
countDownEnd.await();
System.out.println("CountDownLatchExa-Y");
}
public static void main(String[] args) throws InterruptedException
{
CountDownLatchTest test = new CountDownLatchTest();
test.CountDownLatchExa();
}
}
多线程之CountDownLatch
最新推荐文章于 2022-08-24 21:04:46 发布