java CountDownLatch与golang WaitGroup的用法
程序员文章站
2024-01-09 23:08:46
主线程等待所有线程执行结束再继续执行public class TestCountDownLatch { static int loopCount = 10; static CountDownLatch latch = new CountDownLatch(loopCount); static class Hello implements Runnable{ private int i; private Hello(int i){...
主线程等待所有线程执行结束再继续执行
public class TestCountDownLatch {
static int loopCount = 10;
static CountDownLatch latch = new CountDownLatch(loopCount);
static class Hello implements Runnable{
private int i;
private Hello(int i){
this.i = i;
}
@Override
public void run() {
System.out.println("hello i = "+i);
latch.countDown();
}
}
public static void main(String[] args) throws Exception {
for(int i = 0; i < loopCount; i++){
new Thread(new Hello(i)).start();
}
latch.await();
System.out.println("main finish");
}
}
var wg sync.WaitGroup
func hello(i int) {
defer wg.Done()
fmt.Println("hello i = ", i)
}
func main() {
loopCount := 10
wg.Add(loopCount)
for i := 0; i < loopCount; i++ {
go hello(i)
}
wg.Wait()
fmt.Println("main finish")
}
本文地址:https://blog.csdn.net/lovecj111/article/details/110290098
上一篇: 企业有必要上WMS系统吗?为什么?
推荐阅读
-
Java中CyclicBarrier和CountDownLatch的用法与区别
-
java中hashCode方法与equals方法的用法总结
-
Java实现Map集合遍历的四种常见方式与用法分析
-
Java实现的JSONUtil工具类与用法示例
-
java Swing JFrame框架类中setDefaultCloseOperation的参数含义与用法示例
-
java中hashCode方法与equals方法的用法总结
-
Java实现Map集合遍历的四种常见方式与用法分析
-
Java实现的图片高质量缩放类定义与用法示例
-
Java实现的对称加密算法AES定义与用法详解
-
Java实现的对称加密算法3DES定义与用法示例