CountDownLatch api详解
程序员文章站
2022-06-28 18:31:48
今天时间学习CountDownLatchapi,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充package test.java.util.concurrent;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import org.junit.Test;/.....
今天时间学习CountDownLatch api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
/**
* CountDownLatch的测试类
*
* @date 2020-07-14 17:31:12
*/
public class CountDownLatchTest {
class ThreadTest extends Thread{
private CountDownLatch count;
ThreadTest(CountDownLatch count){
this.count=count;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+":"+count.getCount());
count.countDown();
}
}
/**
* 初始化,参数表示线程数量,即AQS中的同步状态
* @Param
*/
@Test
public void testConstruct0()throws Exception{
CountDownLatch testObj=new CountDownLatch(9);
ThreadTest test1=new ThreadTest(testObj);
ThreadTest test2=new ThreadTest(testObj);
ThreadTest test3=new ThreadTest(testObj);
ThreadTest test4=new ThreadTest(testObj);
ThreadTest test5=new ThreadTest(testObj);
ThreadTest test6=new ThreadTest(testObj);
ThreadTest test7=new ThreadTest(testObj);
ThreadTest test8=new ThreadTest(testObj);
ThreadTest test9=new ThreadTest(testObj);
test1.start();
test2.start();
test3.start();
test4.start();
test5.start();
test6.start();
test7.start();
test8.start();
test9.start();
testObj.await();
System.out.println(123);
}
/**
* 一直等待
* @Param
*/
@Test
public void testAwait()throws Exception{
CountDownLatch testObj=new CountDownLatch(3);
ThreadTest test1=new ThreadTest(testObj);
ThreadTest test2=new ThreadTest(testObj);
ThreadTest test3=new ThreadTest(testObj);
test1.start();
test2.start();
test3.start();
testObj.await();
System.out.println(123);
}
/**
* 等待指定时间
* @Param
*/
@Test
public void testAwait1()throws Exception{
CountDownLatch testObj=new CountDownLatch(3);
ThreadTest test1=new ThreadTest(testObj);
ThreadTest test2=new ThreadTest(testObj);
ThreadTest test3=new ThreadTest(testObj);
test1.start();
test2.start();
test3.start();
testObj.await(500,TimeUnit.MILLISECONDS);
System.out.println(123);
}
/**
* 计数器减一操作
* @Param
*/
@Test
public void testCountDown()throws Exception{
CountDownLatch testObj=new CountDownLatch(3);
ThreadTest test1=new ThreadTest(testObj);
ThreadTest test2=new ThreadTest(testObj);
ThreadTest test3=new ThreadTest(testObj);
test1.start();
test2.start();
test3.start();
testObj.await(500,TimeUnit.MILLISECONDS);
System.out.println(123);
}
/**
* 获取当前计数值
* @Param
*/
@Test
public void testGetCount()throws Exception{
CountDownLatch testObj=new CountDownLatch(3);
ThreadTest test1=new ThreadTest(testObj);
ThreadTest test2=new ThreadTest(testObj);
ThreadTest test3=new ThreadTest(testObj);
test1.start();
test2.start();
test3.start();
testObj.await(500,TimeUnit.MILLISECONDS);
System.out.println(123);
}
/**
* toString
* @Param
*/
@Test
public void testToString()throws Exception{
CountDownLatch testObj=new CountDownLatch(3);
System.out.println(testObj.toString());
}
}
本文地址:https://blog.csdn.net/u011106263/article/details/107350077