Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the signal synchronization of Java multithreaded cooperative jobs

Shulou Source: shulou.com Published: 2022-05-31 17:58:39 09月18日 Update

This article introduces the relevant knowledge of "how to realize the signal synchronization of Java multithreaded collaborative operations". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Signal synchronization

Many times, multithreading is a collaborative job. For example, 4 threads on e-commerce data quarterly statistics, after the statistics are completed, and then summarized. How do we know that all four threads have completed execution? we can use the helper class CountDownLatch (

Reduce count), CyclicBarrier (circular fence), Semaphore (semaphore).

Second, based on the time dimension 1, CountDownLatch

How many collaborating threads initialize the construction parameters of the CountDownLatch.

Public static void main (String [] args) throws InterruptedException {final int NUM=6; / / defined total quantity; CountDownLatch countDownLatch=new CountDownLatch (NUM); for (int I = 0; I

< NUM; i++) { new Thread(()->

{System.out.println (Thread.currentThread (). GetName () + "Thread, executed!") ; / / reduce the count countDownLatch.countDown ();}, String.valueOf (item1). Start ();} / wait for all tasks to complete countDownLatch.await (); System.out.println ("all thread tasks have been executed!") ;}

2 、 CyclicBarrier

Similar to reducing the count

Public static void main (String [] args) {final int NUM = 6; / define the number of loops and the tasks after the loop is completed (Runnable API implementation); CyclicBarrier cyclicBarrier = new CyclicBarrier (NUM, ()-> {System.out.println ("all thread tasks have been executed!") ); for (int I = 0; I

< NUM; i++) { new Thread(()->

{System.out.println (Thread.currentThread (). GetName () + "Thread, executed!") ; try {/ / waiting point or obstacle point, waiting for all tasks to be completed, cyclicBarrier.await ();} catch (InterruptedException e) {e.printStackTrace ();} catch (BrokenBarrierException e) {e.printStackTrace () }}, String.valueOf (iTun1) .start ();}}

Third, based on the signal dimension

Semaphore, where m > = n, the thread and signal is MRV n.

Import java.util.Random;import java.util.concurrent.Semaphore;import java.util.concurrent.TimeUnit;/* semaphore * 6 threads to preempt three signals * * / public class SemaphoreDemo {public static void main (String [] args) {Semaphore semaphore=new Semaphore (3); for (int I = 0; I)

< 6; i++) { new Thread(()->

{try {/ / get signal semaphore.acquire (); System.out.println ("Thread:" + Thread.currentThread () .getName () + "- get signal"); int time = new Random () .nextInt (5) System.out.println (("Thread:" + Thread.currentThread () .getName () + "- hibernation" + time+ "s!")) ; TimeUnit.SECONDS.sleep (time);} catch (InterruptedException e) {e.printStackTrace ();} finally {/ / release signal System.out.println ("Thread:" + Thread.currentThread (). GetName () + "- release signal") Semaphore.release ();}}, String.valueOf (iTun1). Start ();}

First, threads 4, 1, and 3 get the signal, and thread 1 releases the signal, and thread 2 gets the signal. After threads 2 and 3 release the signal, threads 5 and 6 get the signal.

This is the end of the content of "how to realize the signal synchronization of Java multithreaded collaborative jobs". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Threads signals tasks collaboration jobs synchronization loops semaphores content quantity more knowledge dimensions statistics practicality learning and then three parameters dilemmas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple vpn Shulou Tech Info Linux