面试官跟我扯了半小时 CountDownLatch 后,给我发 Offer?| 原力计划


责编 | 王晓曼
出品 | CSDN博客

package?onemore.study;
import?java.text.SimpleDateFormat;
import?java.util.Date;
import?java.util.Random;
import?java.util.concurrent.CountDownLatch;
public?class?Customer?implements?Runnable?{
????private?CountDownLatch?latch;
????private?String?name;
????public?Customer(CountDownLatch?latch,?String?name)?{
????????this.latch?=?latch;
????????this.name?=?name;
????}
????@Override
????public?void?run()?{
????????try?{
????????????SimpleDateFormat?sdf?=?new?SimpleDateFormat("HH:mm:ss.SSS");
????????????Random?random?=?new?Random();
????????????System.out.println(sdf.format(new?Date())?+?"?"?+?name?+?"出发去饭店");
????????????Thread.sleep((long)?(random.nextDouble()?*?3000)?+?1000);
????????????System.out.println(sdf.format(new?Date())?+?"?"?+?name?+?"到了饭店");
????????????latch.countDown();
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
}
package?onemore.study;
import?java.text.SimpleDateFormat;
import?java.util.Date;
import?java.util.concurrent.CountDownLatch;
public?class?Waitress?implements?Runnable?{
????private?CountDownLatch?latch;
????private?String?name;
????public?Waitress(CountDownLatch?latch,?String?name)?{
????????this.latch?=?latch;
????????this.name?=?name;
????}
????@Override
????public?void?run()?{
????????try?{
????????????SimpleDateFormat?sdf?=?new?SimpleDateFormat("HH:mm:ss.SSS");
????????????System.out.println(sdf.format(new?Date())?+?"?"?+?name??+?"等待顾客");
????????????latch.await();
????????????System.out.println(sdf.format(new?Date())?+?"?"?+?name??+?"开始上菜");
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
}
package?onemore.study;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.concurrent.CountDownLatch;
public?class?CountDownLatchTester?{
????public?static?void?main(String[]?args)?throws?InterruptedException?{
????????CountDownLatch?latch?=?new?CountDownLatch(3);
????????List<Thread>?threads?=?new?ArrayList<>(3);
????????threads.add(new?Thread(new?Customer(latch,?"张三")));
????????threads.add(new?Thread(new?Customer(latch,?"李四")));
????????threads.add(new?Thread(new?Customer(latch,?"王五")));
????????for?(Thread?thread?:?threads)?{
????????????thread.start();
????????}
????????Thread.sleep(100);
????????new?Thread(new?Waitress(latch,?"?小芳?")).start();
????}
}
15:25:53.015?王五出发去饭店
15:25:53.015?李四出发去饭店
15:25:53.015?张三出发去饭店
15:25:53.062??小芳?等待顾客
15:25:54.341?张三到了饭店
15:25:54.358?李四到了饭店
15:25:56.784?王五到了饭店
15:25:56.784??小芳?开始上菜
latch.await();
latch.await(3,?TimeUnit.SECONDS);
运行结果可能是这样的:
17:24:40.915?张三出发去饭店
17:24:40.915?李四出发去饭店
17:24:40.915?王五出发去饭店
17:24:40.948??小芳?等待顾客
17:24:43.376?李四到了饭店
17:24:43.544?王五到了饭店
17:24:43.951??小芳?开始上菜
17:24:44.762?张三到了饭店
public?CountDownLatch(int?count)?{
????if?(count?<?0)?throw?new?IllegalArgumentException("count?<?0");
????this.sync?=?new?Sync(count);
}
public?void?countDown()?{
????sync.releaseShared(1);
}
public?final?boolean?releaseShared(int?arg)?{
????if?(tryReleaseShared(arg))?{?//对计数器进行减一操作
????????doReleaseShared();//如果计数器为0,唤醒被await方法阻塞的所有线程
????????return?true;
????}
????return?false;
}
protected?boolean?tryReleaseShared(int?releases)?{
????for?(;;)?{//死循环,如果CAS操作失败就会不断继续尝试。
????????int?c?=?getState();//获取当前计数器的值。
????????if?(c?==?0)//?计数器为0时,就直接返回。
????????????return?false;
????????int?nextc?=?c-1;
????????if?(compareAndSetState(c,?nextc))//?使用CAS方法对计数器进行减1操作
????????????return?nextc?==?0;//如果操作成功,返回计数器是否为0
????}
}
public?void?await()?throws?InterruptedException?{
????sync.acquireSharedInterruptibly(1);
}
public?final?void?acquireSharedInterruptibly(int?arg)
????????throws?InterruptedException?{
????if?(Thread.interrupted())
????????throw?new?InterruptedException();
????if?(tryAcquireShared(arg)?<?0)//判断计数器是否为0
????????doAcquireSharedInterruptibly(arg);//如果不为0则阻塞当前线程
}
protected?int?tryAcquireShared(int?acquires)?{
????return?(getState()?==?0)???1?:?-1;
}面试官:嗯,很不错,马上给你发Offer。
版权声明:本文为CSDN博主「万猫学社」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/heihaozi/article/details/105738230

更多精彩推荐
你点的每个“在看”,我都认真当成了喜欢
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
- 1 习近平将发表二〇二六年新年贺词 7904141
- 2 2026年国补政策来了 7808738
- 3 东部战区:开火!开火!全部命中! 7712893
- 4 2026年这些民生政策将惠及百姓 7616985
- 5 小学食堂米线过期2.5小时被罚5万 7519709
- 6 解放军喊话驱离台军 原声曝光 7428214
- 7 为博流量直播踩烈士陵墓?绝不姑息 7327605
- 8 每月最高800元!多地发放养老消费券 7238391
- 9 数字人民币升级 1月1日起将计付利息 7141831
- 10 2026年1月1日起 一批新规将施行 7040675








![南宫cos最新作品图集_小南宫zzZ长隆水上乐园镜流泳装[持续更新]](https://imgs.knowsafe.com:8087/img/aideep/2025/12/6/4e8d1f7708fcdcc5467c7bad5b257aa1.jpg?w=250)
CSDN
