In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the implementation method of sequential execution of three threads". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the implementation method of sequential execution of three threads"?
Let's first talk about the requirements, that is, three threads, assuming that thread 1 is the first to execute, then thread 2 to execute, and then thread 3 to execute, and then how many ways to implement it?
In fact, its essence is to implement, let thread 2Jing 3 wait for thread 1 to finish execution, so the key point is what methods can make thread 2Jing 3 wait.
Join
The first reaction should be to use the join method, because join already supports this mechanism.
For example, if I call the join method of thread An in thread B, thread B will wait until thread A finishes execution.
So how exactly should it be used?
Don't panic. I have an example here. Take a look:
Public class ThreadLoopOne {public static void main (String [] args) {Thread T1 = new Thread (new Work (null)); Thread T2 = new Thread (new Work (T1)); Thread T3 = new Thread (new Work (T2)); t1.start (); t2.start (); t3.start ();} static class Work implements Runnable {private Thread beforeThread Public Work (Thread beforeThread) {this.beforeThread = beforeThread;} @ Override public void run () {/ / if there is a thread, join will come in, and if not, output if (beforeThread! = null) {try {beforeThread.join () System.out.println ("thread start:" + Thread.currentThread (). GetName ());} catch (InterruptedException e) {e.printStackTrace ();}} else {System.out.println ("thread start:" + Thread.currentThread () .getName ()) }
CountDownLatch
As I just said, the essence is to let thread Bjine C wait for thread A to finish execution.
Then semaphores are a good choice.
If you want to make it happen, it's probably like this:
Public class ThreadLoopTwo {public static void main (String [] args) {/ / set the semaphore of thread 1 to 0 CountDownLatch cOne = new CountDownLatch (0); / / set the semaphore of thread 2 to 1 CountDownLatch cTwo = new CountDownLatch (1); / / set the semaphore of thread 3 to 1 CountDownLatch cThree = new CountDownLatch (1) / / because cOne is 0, T1 can directly execute Thread T1 = new Thread (new Work (cOne,cTwo)); / / after thread T1 execution is completed, the cTwo is 0, and T2 starts executing Thread T2 = new Thread (new Work (cTwo,cThree)) / / Thread T2 finishes execution, when cThree is 0, T3 starts executing Thread T3 = new Thread (new Work (cThree,cThree)); t1.start (); t2.start (); t3.start ();} static class Work implements Runnable {CountDownLatch cOne; CountDownLatch cTwo; public Work (CountDownLatch cOne, CountDownLatch cTwo) {super () This.cOne = cOne; this.cTwo = cTwo;} @ Override public void run () {try {/ / the current thread semaphore is 0; System.out.println ("thread start:" + Thread.currentThread () .getName () / / the last thread semaphore minus 1 cTwo.countDown ();} catch (InterruptedException e) {e.printStackTrace ();}
Use a single thread pool
The reason why the execution order of thread 1 and 2 cannot be guaranteed is that some optimizations may be made in the compiler, resulting in no way to execute sequentially.
If we use a single thread pool to execute, there will be no such problem.
Specific implementation:
Public class ThreadLoopThree {public static void main (String [] args) {Thread T1 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("thread start:" + Thread.currentThread () .getName () + "run one");}}) Thread T2 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("thread start:" + Thread.currentThread (). GetName () + "run two");}}) Thread T3 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("thread start:" + Thread.currentThread (). GetName () + "run three");}}); ExecutorService executor = Executors.newSingleThreadExecutor () / / add threads to the thread pool in turn executor.submit (T1); executor.submit (T2); executor.submit (T3); / / close the thread pool in time executor.shutdown ();}}
CompletableFuture
If you use CompletableFuture to implement it, the code is very concise.
Public class ThreadLoopFour {public static void main (String [] args) {Thread T1 = new Thread (new Work ()); Thread T2 = new Thread (new Work ()); Thread T3 = new Thread (new Work ()); CompletableFuture.runAsync (()-> t1.start ()) .thenRun (()-> t2.start ()) .thenRun (()-> t3.start () } static class Work implements Runnable {@ Override public void run () {System.out.println ("thread start:" + Thread.currentThread (). GetName ()); at this point, I believe you have a deeper understanding of "what is the implementation of sequential execution of three threads". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.