Java provides a handy tool for controlling the order in which threads are executed, allowing us to control the execution of threads more flexibly by means of these handy tools.
Today we mainly discuss several common ways to control threads: join, sleep, yield, and how to adjust the thread's priority.
Join Threads
Thread provides a way for a thread to wait for another thread to complete and then execute itself: the Join method. When the join method of another thread is invoked in a program execution stream, the calling thread is blocked until the join thread joined by the join method completes.
Thread Sleep
If we need to suspend the currently executing thread for a period of time and go into a blocking state, you can call the thread class's static method Sleep method.
Thread Yield
The yield method is a somewhat similar approach to the sleep method, and it is also a static method provided by the thread class, which also allows the currently executing thread to pause, but it does not block the thread, it simply moves the thread into a ready state. Yield just let the current thread pause to have the system's thread scheduler reschedule once, and it is entirely possible that when a thread calls the yield method to pause, the thread scheduler again dispatches it to execute again.
In fact, when a thread calls the yield method pause, only the priority is the same as the current thread, or a thread with a ready state that has a higher priority than the current thread is likely to be given an opportunity to execute.
So what is the difference between the sleep method and the yield method?
The 1sleep method pauses the current thread, giving the other threads an opportunity to execute, and does not care whether the priority of other threads is Cheng or lower than the current line. The yield method, however, only gives the same priority or higher priority to the thread execution opportunity.
The 2sleep method moves the thread into a blocking state until it is blocked until it is ready, and the yield method is directly forced into the ready state without blocking. It is therefore entirely possible that after a thread invokes the yield method, it immediately obtains the processor resources again and executes.
The 3sleep method declaration throws a interruptedexception exception, so either the exception is caught when the sleep method is invoked, or the exception is explicitly declared. The yield method, however, does not necessarily declare any exceptions thrown.
The 4sleep method has better portability than the yield method, and it is generally not recommended to use the yield method to control the execution of concurrent threads.
Change Thread Priority
The priority of the thread is mentioned when the yield method is described above. So how do you set the priority of the thread?
Each thread defaults to the same priority as the parent that created it, and by default, the main thread has a normal priority, and a child thread created by the main thread also has a normal priority.
Thread provides the setpriority (intnewpriority) and GetPriority () methods to set and return the priority of the specified thread, where the parameter of the SetPriority method can be an integer, range 1-10, You can also use the three static constants of the thread class: Max_priority=10,min_priority=1 and Norm_priority=5 respectively.
Background Process
Finally, the background process, as the name implies is running in the background of the thread, his task is to provide services to other threads, this thread is called Daemon thread, also known as a Daemon or wizard thread. The JVM's garbage collection thread is a typical background thread. Background threads have a feature: if all foreground threads are dead, background threads will automatically die.
The
call the Setdaemon (True) method of the thread object to set the specified thread as a background thread. However, it is important to note that the Setdaemon method must be invoked before the Start method before it can be successfully set as a background thread. There is also a method--isdaemon () that determines whether the specified thread is a background process. When only the background process is left in the entire virtual machine, the program does not continue to run, so the virtual machine exits.