Conversion of Java thread State

Source: Internet
Author: User

Java Threads: Transition of thread StateOne, thread state 1, new state: The thread object has been created, and the start () method has not been invoked on it. 2. Operational status: When the thread is eligible to run, but the scheduler has not selected it as the state in which the thread is running. When the start () method is called, the thread first enters the operational state. After the thread has run or has returned from blocking, waiting, or sleeping, it returns to the operational state. 3. Running state: The thread Scheduler selects a thread from a running pool as the current thread is in the state of the thread. This is also the only way that a thread goes into a running state. 4. Wait/block/Sleep state: This is the state at which the thread is eligible to run. In fact, this three-state combination is one, and its common denominator is that the thread is still alive, but there is no condition to run at the moment. In other words, it is operational, but if an event occurs, he may return to the operational state. 5. Death state: When the thread's run () method finishes, it is considered dead. This thread object may be alive, but it is not a separate thread. Once a thread dies, it cannot be resurrected. If you call the start () method on a dead thread, the java.lang.IllegalThreadStateException exception is thrown. For a detailed status conversion diagram can be seen in my "Java Multithreaded programming summary" in figure II, to prevent the thread to perform the blocking of threads, consider three aspects, regardless of the IO blocking situation: Sleep, wait, because the lock of an object needs to be blocked. 1, Sleep thread.sleep (long Millis) and Thread.Sleep (long millis, int Nanos) static methods force the currently executing thread to hibernate (pause execution) to "slow down the thread." When the thread sleeps, it sleeps somewhere and does not return to the operational state until it wakes up. When the sleep time expires, it returns to the operational state. Reason for thread sleep: Thread execution is too fast, or it needs to be forced into the next round because Java specifications do not guarantee a reasonable rotation.        Sleep implementation: Call a static method. try {
Thread.Sleep (123);
} catch (Interruptedexception e) {
E.printstacktrace ();
The location of sleep: In order for other threads to have an opportunity to execute, the call to Thread.Sleep () can be placed within the thread run (). This ensures that the thread will sleep during execution. For example, in the previous example, a time-consuming operation was changed to sleep to slow down the execution of the thread. You can write this: public void run () {
for (int i = 0;i<5;i++) {//very time consuming operation to slow down the execution of the thread
For (long k= 0; k <100000000;k++);
try {
Thread.Sleep (3);
} catch (Interruptedexception e) {
E.printstacktrace (); .
}

System.out.println (This.getname () + ":" +i);
}
The result of the operation: a: 0
John Doe: 0
A three: 1
A three: 2
A three: 3
John Doe: 1
John Doe: 2
A three: 4
John Doe: 3
John Doe: 4

Process finished with exit code 0 this way, the thread always sleeps 3 milliseconds during each execution, sleeps, and the other threads have a chance to execute. Note: 1. Thread sleep is the best way to help all threads get the chance to run. 2, thread sleep expires automatically wake up and return to the operational state, not the running state. The time specified in sleep () is the shortest time the thread will not run. Therefore, the sleep () method does not guarantee that the thread will start executing after the sleep expires. 3. Sleep () is a static method that only controls the currently running thread. Here's an example:/**
* A counter, count to 100, pause between each number for 1 seconds, output a string every 10 digits
*
* @author leizhimin 2008-9-14 9:53:49
*/
PublicclassMyThreadextendsThread {

PublicvoidRun () {
for(inti = 0; I < 100; i++) {
if((i)% 10 = = 0) {
System.out.println ("-------"+ i);
}
System.out.print (i);
Try{
Thread.Sleep (1);
System.out.print ("thread sleeps 1 milliseconds!" \ n ");
}Catch(Interruptedexception e) {
E.printstacktrace ();
}
}
}

PublicStaticvoidMain (string[] args) {
NewMyThread (). Start ();
}
}-------0
0 Threads sleep 1 milliseconds!
1 threads sleep 1 milliseconds!
2 threads sleep 1 milliseconds!
3 threads sleep 1 milliseconds!
4 threads sleep 1 milliseconds!
5 threads sleep 1 milliseconds!
6 threads sleep 1 milliseconds!
7 Threads sleep 1 milliseconds!
8 threads sleep 1 milliseconds!
9 Threads sleep 1 milliseconds!
-------10
10 threads sleep 1 milliseconds!
11 Threads sleep 1 milliseconds!
12 threads sleep 1 milliseconds!
13 Threads sleep 1 milliseconds!
14 Threads sleep 1 milliseconds!
15 Threads sleep 1 milliseconds!
16 Threads sleep 1 milliseconds!
17 Threads sleep 1 milliseconds!
18 Threads sleep 1 milliseconds!
19 Threads sleep 1 milliseconds!
-------20
20 threads sleep 1 milliseconds!
21 Threads sleep 1 milliseconds!
22 Threads sleep 1 milliseconds!
23 Threads sleep 1 milliseconds!
24 Threads sleep 1 milliseconds!
25 threads sleep 1 milliseconds!
26 Threads sleep 1 milliseconds!
27 Threads sleep 1 milliseconds!
28 Threads sleep 1 milliseconds!
29 Threads sleep 1 milliseconds!
-------30
30 threads sleep 1 milliseconds!
31 Threads sleep 1 milliseconds!
32 threads sleep 1 milliseconds!
33 Threads sleep 1 milliseconds!
34 Threads sleep 1 milliseconds!
35 Threads sleep 1 milliseconds!
36 Threads sleep 1 milliseconds!
37 Threads sleep 1 milliseconds!
38 Threads sleep 1 milliseconds!
39 Threads sleep 1 milliseconds!
-------40
40 threads sleep 1 milliseconds!
41 Threads sleep 1 milliseconds!
42 Threads sleep 1 milliseconds!
43 Threads sleep 1 milliseconds!
44 Threads sleep 1 milliseconds!
45 threads sleep 1 milliseconds!
46 Threads sleep 1 milliseconds!
47 Threads sleep 1 milliseconds!
48 Threads sleep 1 milliseconds!
49 Threads sleep 1 milliseconds!
-------50
50 threads sleep 1 milliseconds!
51 Threads sleep 1 milliseconds!
52 Threads sleep 1 milliseconds!
53 Threads sleep 1 milliseconds!
54 Threads sleep 1 milliseconds!
55 Threads sleep 1 milliseconds!
56 Threads sleep 1 milliseconds!
57 Threads sleep 1 milliseconds!
58 Threads sleep 1 milliseconds!
59 Threads sleep 1 milliseconds!
-------60
60 threads sleep 1 milliseconds!
61 Threads sleep 1 milliseconds!
62 Threads sleep 1 milliseconds!
63 Threads sleep 1 milliseconds!
64 threads sleep 1 milliseconds!
65 Threads sleep 1 milliseconds!
66 Threads sleep 1 milliseconds!
67 Threads sleep 1 milliseconds!
68 Threads sleep 1 milliseconds!
69 Threads sleep 1 milliseconds!
-------70
70 threads sleep 1 milliseconds!
71 Threads sleep 1 milliseconds!
72 Threads sleep 1 milliseconds!
73 Threads sleep 1 milliseconds!
74 Threads sleep 1 milliseconds!
75 threads sleep 1 milliseconds!
76 Threads sleep 1 milliseconds!
77 Threads sleep 1 milliseconds!
78 Threads sleep 1 milliseconds!
79 Threads sleep 1 milliseconds!
-------80
80 threads sleep 1 milliseconds!
81 threads sleep 1 milliseconds!
82 Threads sleep 1 milliseconds!
83 Threads sleep 1 milliseconds!
84 threads sleep 1 milliseconds!
85 Threads sleep 1 milliseconds!
86 threads sleep 1 milliseconds!
87 Threads sleep 1 milliseconds!
88 Threads sleep 1 milliseconds!
89 Threads sleep 1 milliseconds!
-------90
90 threads sleep 1 milliseconds!
91 Threads sleep 1 milliseconds!
92 Threads sleep 1 milliseconds!
93 Threads sleep 1 milliseconds!
94 Threads sleep 1 milliseconds!
95 threads sleep 1 milliseconds!
96 Threads sleep 1 milliseconds!
97 threads sleep 1 milliseconds!
98 threads sleep 1 milliseconds!
99 threads sleep 1 milliseconds!

Process finished with exit code 0
2, thread priority and thread concession yield () thread concessions are through thread. yield() to achieve. The yield () method acts by suspending the currently executing thread object and executing other threads. To understand yield (), you must understand the concept of thread precedence. The thread always has a priority and the priority range is between 1~10. The JVM thread Scheduler is a priority-based preemptive scheduling mechanism. 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 that's just the majority of the situation. 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. Set the priority of the thread: The default priority of the 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: Thread t = new MyThread ();
T.setpriority (8);
T.start (); The thread 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. The thread default priority is three constants in the 5,thread class that define the thread priority range: Static int max_priority
The highest priority a thread can have.
static int min_priority
The lowest priority a thread can have.
static int norm_priority
The default priority assigned to the thread. 3. The Thread.yield () method Thread.yield () method acts by suspending the currently executing thread object and executing other threads. Yield () should be done to get the current running thread back to the operational state to allow other threads with the same priority to get the run opportunity. Therefore, 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. Conclusion: Yield () never causes the thread to go to the wait/sleep/block state. In most cases, yield () will cause the thread to go from the running state to the operational state, but it may not be effective. 4, Join () method The non-static method for thread join () lets one thread B "join" to the tail of another thread A. b does not work until a is complete. For example: Thread t = new MyThread ();
T.start ();
T.join (); In addition, the join () method also has an overloaded version with a time-out limit. For example, T.join (5000); Let the thread wait for 5000 milliseconds, and if it exceeds this time, stop waiting and become a running state. Summary to the current location, describes the thread left the running state of 3 methods: 1, Call Thread.Sleep (): The current thread to sleep at least how many 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. In addition to the above three ways, there are several special cases that may leave the thread out of the running state: 1, the thread's run () method is complete. 2. Call the Wait () method on the object (not called on the thread). 3. The thread cannot get a lock on the object, it is trying to run the object's method code. 4. The thread scheduler can decide to move the current state of operation to a running state so that another thread gets a run opportunity without any justification.

Conversion of Java thread State

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.