In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.