How to use CountDownLatch in Java
Editor to share with you how to use CountDownLatch in Java. I hope you will get something after reading this article. Let's discuss it together.
Brief introduction
What's it for? When you call multiple threads in the method and do some unknown operations on the database, there is one more operation that needs to be left to the former, so CountDownLatch is needed.
Practice code package com.github.gleans
Import java.util.concurrent.CountDownLatch
Public class TestCountDownLatch {
Public static void main (String [] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch (3)
New KeyPass (1000L, "thin jack", latch). Start ()
New KeyPass (2000L, "noral jack", latch). Start ()
New KeyPass (3000L, "fat jack", latch) .start ()
Latch.await ()
System.out.println ("here is the last insert to the database ~")
}
Static class KeyPass extends Thread {
Private long times
Private CountDownLatch countDownLatch
Public KeyPass (long times, String name, CountDownLatch countDownLatch) {
Super (name)
This.times = times
This.countDownLatch = countDownLatch
}
@ Override
Public void run () {
Try {
System.out.println ("operator:" + Thread.currentThread (). GetName ()
+ "insert database, duration: + this.times / 1000 +" seconds ")
Thread.sleep (times)
CountDownLatch.countDown ()
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
}
}
Illustration use await () to end the operation package com.github.gleans early
Import java.util.concurrent.CountDownLatch
Import java.util.concurrent.TimeUnit
Public class TestCountDownLatch {
Public static void main (String [] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch (3)
New KeyPass (2000L, "Company one", latch). Start ()
New KeyPass (3000L, Company II, latch). Start ()
New KeyPass (5000L, "Company III", latch). Start ()
Latch.await (2, TimeUnit.SECONDS)
System.out.println ("Chief Jia PPT Tour ~")
System.out.println ("financing is completed, flowers are scattered")
}
Static class KeyPass extends Thread {
Private long times
Private CountDownLatch countDownLatch
Public KeyPass (long times, String name, CountDownLatch countDownLatch) {
Super (name)
This.times = times
This.countDownLatch = countDownLatch
}
@ Override
Public void run () {
Try {
Thread.sleep (times)
System.out.println ("responsible person:" + Thread.currentThread (). GetName ()
+ "start work, duration:" + this.times / 1000 + "seconds")
CountDownLatch.countDown ()
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
}
}
Suppose company 1, company 2 and company 3 each need 2s, 3s and 5s to complete the work, and Mr. Jia can only wait 2s, so set the await timeout.
Latch.await (2, TimeUnit.SECONDS)
Execution result
Responsible person: as soon as the company starts working, the duration: 2 seconds.
~ ~ Jia Boss PPT Tour ~
The financing is completed and the flowers are scattered.
Responsible person: company two starts work, duration: 3 seconds
Responsible person: the company starts work on the third day, lasting for 5 seconds.
Method description after reading this article, I believe you have some understanding of "how to use CountDownLatch in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!