countDownLatch学习

编程

使用匿名内部类创建线程:

new Thread(() -> {

while (true) {

try {

Thread.sleep(3000);

System.out.println("线程1开始执行…… ……");

} catch (Exception e) {

e.printStackTrace();

}

}

}).start();

 

CountDownLatch测试

public static void main(String[] args) {

final CountDownLatch latch = new CountDownLatch(2);

System.out.println("主线程开始执行…… ……");

//第一个子线程执行

ExecutorService es1 = Executors.newSingleThreadExecutor();

es1.execute(() -> {

try {

Thread.sleep(3000);

System.out.println("子线程:" + Thread.currentThread().getName() + "执行");

} catch (InterruptedException e) {

e.printStackTrace();

}

latch.countDown();

});

es1.shutdown();

//第二个子线程执行

ExecutorService es2 = Executors.newSingleThreadExecutor();

es2.execute(() -> {

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("子线程:" + Thread.currentThread().getName() + "执行");

latch.countDown();

});

es2.shutdown();

System.out.println("等待两个线程执行完毕…… ……");

try {

latch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("两个子线程都执行完毕,继续执行主线程");

}

运行结果;

主线程开始执行…… ……

等待两个线程执行完毕…… ……

子线程:pool-1-thread-1执行

子线程:pool-2-thread-1执行

两个子线程都执行完毕,继续执行主线程

 

模拟并发示例

public static void main(String[] args) {

ExecutorService pool = Executors.newCachedThreadPool();

CountDownLatch cdl = new CountDownLatch(100);

for (int i = 0; i < 100; i++) {

CountRunnable runnable = new CountRunnable(cdl);

pool.execute(runnable);

}

}

}

class CountRunnable implements Runnable {

private CountDownLatch countDownLatch;

public CountRunnable(CountDownLatch countDownLatch) {

this.countDownLatch = countDownLatch;

}

@Override

public void run() {

try {

synchronized (countDownLatch) {

/*** 每次减少一个容量*/

countDownLatch.countDown();

System.out.println("thread counts = " + (countDownLatch.getCount()));

}

countDownLatch.await();

System.out.println("concurrency counts = " + (100 - countDownLatch.getCount()));

} catch (InterruptedException e) {

e.printStackTrace();

}

}

执行结果
thread counts = 99
thread counts = 98
thread counts = 97
thread counts = 96
thread counts = 95
thread counts = 94
thread counts = 93
thread counts = 92
thread counts = 91
thread counts = 90
thread counts = 89
thread counts = 88
thread counts = 87
thread counts = 86
thread counts = 85
thread counts = 84
thread counts = 83
thread counts = 82
thread counts = 81
thread counts = 80
thread counts = 79
thread counts = 78
thread counts = 77
thread counts = 76
thread counts = 75
thread counts = 74
thread counts = 73
thread counts = 72
thread counts = 71
thread counts = 70
thread counts = 69
thread counts = 68
thread counts = 67
thread counts = 66
thread counts = 65
thread counts = 64
thread counts = 63
thread counts = 62
thread counts = 61
thread counts = 60
thread counts = 59
thread counts = 58
thread counts = 57
thread counts = 56
thread counts = 55
thread counts = 54
thread counts = 53
thread counts = 52
thread counts = 51
thread counts = 50
thread counts = 49
thread counts = 48
thread counts = 47
thread counts = 46
thread counts = 45
thread counts = 44
thread counts = 43
thread counts = 42
thread counts = 41
thread counts = 40
thread counts = 39
thread counts = 38
thread counts = 37
thread counts = 36
thread counts = 35
thread counts = 34
thread counts = 33
thread counts = 32
thread counts = 31
thread counts = 30
thread counts = 29
thread counts = 28
thread counts = 27
thread counts = 26
thread counts = 25
thread counts = 24
thread counts = 23
thread counts = 22
thread counts = 21
thread counts = 20
thread counts = 19
thread counts = 18
thread counts = 17
thread counts = 16
thread counts = 15
thread counts = 14
thread counts = 13
thread counts = 12
thread counts = 11
thread counts = 10
thread counts = 9
thread counts = 8
thread counts = 7
thread counts = 6
thread counts = 5
thread counts = 4
thread counts = 3
thread counts = 2
thread counts = 1
thread counts = 0
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
concurrency counts = 100
Disconnected from the target VM, address: "127.0.0.1:54856", transport: "socket"

Process finished with exit code 0
 

以上是 countDownLatch学习 的全部内容, 来源链接: utcz.com/z/512053.html

回到顶部