Thread state transitions are the basis of thread control, so let's look at a
1. NEW: A new Thread object has been created.
2. Operational (runnable): After the thread object is created, other threads (such as the main thread) call the object's start () method. The thread in this state is in a pool of running threads, waiting for the thread to be dispatched to get the CPU right.
3. Run (running): The Running State (runnable) thread obtains the CPU time slice (TimeSlice) and executes the program code.
4. Block: Blocking state refers to a thread that has given up the CPU for some reason, or let the CPU TimeSlice, temporarily stop running. Until the thread enters the operational (runnable) state, there is a chance to get the CPU TimeSlice again to the run (running) state. There are three types of blocking:
. Wait for blocking: the thread that runs (running) executes the o.wait () method, and the JVM puts the thread into the wait queue (the waitting queues).
. Synchronous blocking: When a thread running (running) acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread.
. Other blocking: The thread that runs (running) executes the thread.sleep (long ms) or T.join () method, or when an I/O request is made, the JVM puts the thread in a blocking state. When the sleep () state time-out, join () waits for the thread to terminate or times out, or the I/O process is complete, the thread is re-transferred to the operational (runnable) state.
5. Death (dead): The thread Run (), main () method execution ends, or the run () method is exited due to an exception, the thread ends the life cycle. The thread of death cannot be resurrected again.
First, Thread priority
Threads always have priority, and the JVM thread scheduler is a priority-based preemptive scheduling mechanism. The priority is a positive integer between 1~10, and the JVM never changes the priority of a thread, however, the value between 1~10 is not guaranteed. While some JVMs may not recognize 10 different values, and each two or more of these priorities are merged into less than 10 priority, two or more priority threads may be mapped to a priority. In most cases, the currently running thread priority will be greater than or equal to the priority of any thread in the thread pool, but this is only the case in most cases. Note: When designing multithreaded applications, be sure not to rely on the priority of the thread. Because thread scheduling priority operations are not guaranteed, thread precedence can only be used as a way to improve program efficiency, but ensure that the program does not rely on such operations. When thread pool threads have the same priority, the Scheduler's JVM implementation is free to select the thread it likes. There are two possible actions for the scheduler: one is to select a thread to run until it blocks or runs. The second is the Time Shard, which provides equal running opportunities for each thread in the pool. The default priority of a thread is the priority of the execution thread that created it. The priority of a thread can be rerouted through setpriority (int newpriority). For example:
New MyThread (); T.setpriority (8); T.start () ;
The thread default priority is that there are three constants in the 5,thread class that define the thread priority range:
public final static int min_priority = 1; public final static int norm_priority = 5; public final static int Max_priority = ten;
Second, what is a daemon thread
There are two types of threads in a Java thread, one is the user thread, and the other is the daemon thread (the daemon thread). The role of Daemon is to provide services for the operation of other threads, such as GC threads. In fact, the user thread and the daemon thread daemon is essentially no different, the only difference is in the virtual machine's departure: If the user thread is all evacuated, then daemon thread is not a good service, So the virtual machine also exits. The daemon thread is not available inside the virtual machine, and the user can set the daemon thread by itself:
Public final void Setdaemon (Boolean on);
But there are a few things to note:
Thread.setdaemon (TRUE) must be set before Thread.Start () or run out of a illegalthreadstateexception exception.
You cannot set a running regular thread as a daemon thread.
The new thread that is generated in the daemon thread is also daemon.
Not all applications can be assigned to daemon threads for service, such as read-write operations or computational logic. The virtual machine may have exited because the daemon thread has not come and is operating.
Note: There is a clear difference from the daemon, the daemon is created, let the process get rid of the original session control + let the process out of the original Process group Control + Let the process out of the original control terminal control; the Daemon fork () is no longer a daemon, although it copies the process-related information of the parent process. , but the parent process of the subprocess process is not the INIT process, so the so-called Daemon essentially says "parent process hangs, init adoption"
Third, the thread leaves the running state of 3 ways:
1. Call Thread.Sleep (): Causes the current thread to sleep at least a few milliseconds (although it may be interrupted before the specified time).
2. Call Thread.yield (): There is no guarantee for too many things, although it usually brings the current running thread back to the operational state, allowing threads with the same priority to execute.
3. Call the Join () method: Ensure that the current thread stops executing until the thread to which the thread is joined is complete. However, if the thread it joins does not survive, the current thread does not need to stop.
Iv. How to stop a thread
Stopping a thread is an important technical point in multithreaded development, and mastering this technique can effectively handle the termination of a thread. Stopping a thread can use the Thread.stop () method, but it's best not to. This method is not secure and has been deprecated.
There are 3 ways to terminate a running thread in Java:
Use the exit flag to cause the thread to exit normally, that is, the thread terminates when the Run method completes
Use the Stop method to forcibly terminate a thread, but this method is not recommended because stop and suspend and resume are obsolete methods, and they can produce unpredictable results.
Use the interrupt method to break a thread, but this does not terminate a running thread, but it also requires a decision to complete the termination of the threads.
V. Yield () why haven't we used it?
To understand yield (), you must understand the concept of thread precedence. The purpose of using yield () is to allow appropriate rotation between threads of the same priority. However, in practice there is no guarantee that yield () will be compromised, as the thread of the concession may be checked again by the thread scheduler. Calling the yield method does not cause the thread to go into a blocking state, but instead allows the thread to return to the ready state, which only needs to wait for the CPU execution time to be fetched again, unlike the sleep method.
Six, why we use the Join () method
In many cases, the main thread creates and initiates threads, and if a child thread takes a large amount of time-consuming operations, the main thread tends to end before the child thread ends. At this point, if the main thread wants to wait for the child thread to finish before it finishes, such as a child thread processing a data, the main thread will have to get the value in this data, it will use the join () method. The function of the join () method is to wait for the thread object to be destroyed.
As for the wait and notify method, we will discuss it again.
Java Thread State