Control of threads in Java multi-thread
- 7 very important states in a thread:
Initial new, can run runnable, run running, block blocked, lock pool lock_pool, wait queue wait_pool, end dead
If both "Lock pool" and "Wait queue" are considered special cases of "blocking" state, you can summarize threads into 5 states:
New, ready, running, blocked, dead.
┌--------------------< blocking
↓ (1) (2) (3) End
①②③os Dispatch ↑↑
Initial-------> Operational?---------------? Run >-----------┤
T.start () Start ↑↓↓o.wait ()
└-----< locker ←---------<┘←-------< wait queue
Get lock flag synchronized (o)
Note: The markers in the figure are sequentially
① input completed; ②wake UP③T1 exit
⑴ waits for input (the input device is processed and the cup is not processed), the block is placed until the input is complete.
⑵ thread Sleep Sleep ()
⑶t1.join () joins T1 to the run queue until T1 exits, and the current line friend continues.
Special Note: The ①②③ and the ⑴⑵⑶ are one by one corresponding.
Hibernation of the process: Thread.Sleep (1000);//parentheses in milliseconds
When the thread finishes running, the CPU discards the time slice and continues running other programs even when the time slice is not exhausted at the end.
T1.join is actually a parallel thread that runs concurrently.
- To achieve control over multithreading:
To control multi-threading, remember that the following APIs are sufficient:
1,start method: Creates a thread. It is important to note that when you new a thread, it simply means that the thread is in a new state, not an execution state. Starting a thread after using the Start method, the thread is in a ready state, it is not directly running, it means that the thread can run, but when it starts running, it depends on the scheduling of the thread scheduler in the JVM.
2,stop method: Ends a thread. It is easy to lead to deadlocks, not recommended. It is important to note that you should not attempt to invoke the Start method again in case a thread has died, and it will be reported illegalthreadstateexception wrong.
3,join method: Add a thread to a thread. When another thread's join method is called in a program execution flow, the calling thread is blocked until the join thread joined by the join method completes.
4,sleep method: Let the thread sleep. Causes the currently executing thread to pause for a period of time and into a blocking state.
5,yield method: Similar to the Sleep method, let the currently executing thread pause, but he does not block the thread, it simply transfers the thread to a ready state.
6,setpriority method: Sets the thread priority. Set and getpriority sets and gets the priority of the thread, 1 to 101 a total of 10 digits, the default is 5, the higher the number the higher the priority, the easier to get execution opportunities. For cross-platform, it is best not to use precedence to determine the order in which threads are executed. This time use 3 static constants: Max_priority,min_priority,norm_priority.
7,setdaemon method: Sets the background thread. The Daemon Threads (Daemon thread) is a service thread, and when all other threads end, only the Daemon thread is left, and the virtual opportunity exits immediately.
Thread t = new Daemonthread ();
T.setdaemon (True);//setdaemon (true) flags the thread as daemon, and the rest is the same as the normal thread
T.start ();//Must be Setdaemon (true) before starting the thread
Threads that are started within a daemon thread are designated as daemon threads
8,wait (), the Notify () method is described in detail when synchronizing locks are introduced later. In the running state, the thread calls wait (), indicating that the thread will release all of its lock tokens while entering the waiting queue for the object. The status of the wait queue is also blocked, except that the thread frees its own lock tag. After the Notify () method is called, follow the position of Wait (); If a thread calls the object's notify (), it is a thread that notifies the object to wait for the queue to dequeue. Enter the lock pool. If you use Notifyall (), notify all threads in the wait queue to be queued.
- There is a problem, under what circumstances, a thread will go into a blocking state?
1, the thread calls the sleep method and voluntarily discards the processor resources it occupies
2, the thread calls the blocking Io method, and before the method returns, the thread blocks the
3, the thread attempts to obtain a synchronization listener, but the synchronization monitor is being held by another thread
4, the thread waits for a notify notification
5, the thread calls the Suspend method to suspend the thread.
If you let a thread change from a blocking state to a ready state, the last point is to use the Resume method instead. It is worth noting that suspend () pushes the running state to a blocking state (note that the lock flag is not released). Resume status with resume (). Stop () Releases all. There are deprecated flags on these methods, which indicate that this method is not recommended for use. In general, the Main method main () ends when the thread ends, but there may be situations where the thread needs to be interrupted. For multithreading in general, each thread is a loop, and if you break the thread we must find a way to get it out. If you want to end a thread in a block (such as sleep or wait), you can call interrupt () on its object by another thread. The exception interrupted is thrown for blocking (or lock pooling).
Control of threads in Java multi-thread