Preface:
The test last week raised a bug to the module developed by the development colleagues, and it was still a coincidence.
After carefully checking the code, I found that multi-threading is enabled in the business, and 2 threads run at the same time, but the newly started two threads must ensure that one is completed and the other continues to run to eliminate the bug.
When to use it?
Multithreading is used in many places, but if we want to start another thread after a specific thread is completed, CountDownLatch can come in handy.
How to use it?
Let’s take a look at ordinary multithreaded code:
package code;public class MyThread extends Thread { public static void main(String[] args) { MyThread th = new MyThread(); Thread t1 = new Thread(th, "Mythread"); t1.start(); System.out.println(Thread.currentThread().getName()); } public void run() { Mythread1 th2 = new Mythread1(); Thread t2 = new Thread(th2, "Mythread1"); t2.start(); System.out.println(this.currentThread().getName()); } class Mythread1 extends Thread { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(this.currentThread().getName()); } } }The code is as above. First, use MyThread to inherit the Thread class, and then write another MyThread1 class inside the MyThread class, which also inherits the Thread class, and lets it sleep for 1 second in the run method. In this way, the code will be printed:
From the above output order, we can see that the main thread is first started, and then the MyThread thread is started. In the MyThread thread, the MyThread1 thread is started. However, because MyThread1 thread slept for 1 second, it simulated processing of subsequent business, so it would be a little later than MyThread was completed.
Now, add CountDownLatch to the code, and let MyThread1 run first, and then let MyThread continue to run.
package code;import java.util.concurrent.CountDownLatch;public class MyThread extends Thread { CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) { MyThread th = new MyThread(); Thread t1 = new Thread(th, "Mythread"); t1.start(); System.out.println(Thread.currentThread().getName()); } public void run() { Mythread1 th2 = new Mythread1(); Thread t2 = new Thread(th2, "Mythread1"); t2.start(); try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(this.currentThread().getName()); } class Mythread1 extends Thread { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(this.currentThread().getName()); countDownLatch.countDown(); } } }The code writing method is shown above, roughly divided into three steps
1. Let’s first new CountDownLatch object parameter set to 1 (I personally understand this is like new array. When the array is cleared, then the interrupted thread can continue to run)
2. Call countDownLatch.await() in the MyThread class; to stop running the current thread.
3. Call the countDownLatch.countDown() method in Mythread1 class. When all Mythread1 is executed, and the method is finally called, the function is to clear the "array" I mentioned.
Check out the print results of the output
The results are as shown in the figure above, which are in line with the expected results.
Finally, let’s talk about CountDownLatch countDownLatch = new CountDownLatch(1). Set 1 in this area, so you need to call countDownLatch.countDown() once to subtract 1.
If it is another number, the corresponding number of times must be called, otherwise the thread calling countDownLatch.await() will not be executed.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.