当前位置:网站首页>[thread / multithread] execution sequence of threads
[thread / multithread] execution sequence of threads
2022-06-13 05:04:00 【A Xiaobing】
Preface
public class ThreadSort { public static void main(String[] args) { Thread thread1 = new Thread(() -> { System.out.println(" Threads 1"); }); Thread thread2 = new Thread(() -> { System.out.println(" Threads 2"); }); Thread thread3 = new Thread(() -> { System.out.println(" Threads 3"); }); thread1.start(); thread2.start(); thread3.start(); } }The code is simple , Three threads are declared , Print the text separately , Students who are idle and have nothing to do can run for many times , The order of each print , Not at all 123, And out of order , So this means :start() The sequence of method calls cannot determine the execution order of the thread
Threads execute sequentially
The scenario of executing threads in code order is not uncommon , Let's look at the code above , How to make him according to start() The call is executed in sequence ?
The answer is : Use Thread Class join() Method to ensure the execution order
public class ThreadSort { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(() -> { System.out.println(" Threads 1"); }); Thread thread2 = new Thread(() -> { System.out.println(" Threads 2"); }); Thread thread3 = new Thread(() -> { System.out.println(" Threads 3"); }); thread1.start(); thread1.join(); thread2.start(); thread2.join(); thread3.start(); thread3.join(); } }Then run it many times and you will find , The result is always a thread 123 The order of
join() Method explanation
To explain how he realized the sequence , Then you need to enter the source code to have a look , Attach source code
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }Look at the source code , In fact, you can see , This join Method used synchronized To modify , This means that this method can only be called by one instance or method at a time , But when we call above, there is no input parameter , Say it will come if (millis == 0) In this logic , This method is mainly used to determine whether the thread has been started and is active , If it is active , Call wait() Method , So let's continue to look at wait() Method
public final native void wait(long timeout) throws InterruptedException;Call this wait() when , Makes the main thread wait , Wait for the execution of the child thread to complete before continuing the downward execution
边栏推荐
- Implementing the driver registration initcall mechanism on stm32
- C language learning log 2.19
- QT client development -- driver loading problem of connecting to MySQL database
- Explain the differences and usage scenarios between created and mounted
- Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
- Analysis on the similarities and differences of MVC, MVP and mvvc
- Luogu p1012 guess
- QT interface rendering style
- Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
- Advantages of win8.1 and win10
猜你喜欢

Luogu p1036 number selection

LeetCode第297场周赛(20220612)

Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析

Shell built-in string substitution

josephus problem

Must know must know -c language keywords
![[leetcode]- binary search](/img/7f/7d1f616c491c6fb0be93f591da6df1.png)
[leetcode]- binary search

metaRTC4.0稳定版发布

Hidden implementation and decoupling, knowing Pimpl mode

Simple SR: best buddy Gans for highly detailed image super resolution
随机推荐
Force buckle 25 A group of K flipped linked lists
Simple SR: best buddy Gans for highly detailed image super resolution
CMB's written test -- data analysis
Clause 32: moving objects into closures using initialization capture objects
Violence enumeration~
【线程/多线程】线程的执行顺序
Win8.1和Win10各自的优势
Nonstandard compiler attribute extension
17.6 unique_lock详解
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation
QT using layout manager is invalid or abnormal
PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
Chapter 2 process management
Clause 48: understand template metaprogramming
Explain the role of key attribute in V-for
josephus problem
Opencv image storage and reading
LeetCode第297场周赛(20220612)
Embedded hardware: electronic components (1) resistance capacitance inductance