The five States of a thread can be divided into new, dead, runable, running, and blocked states. It can be represented in the following figure:
1. Initial State: A thread object has been created, but no thread executes the START () method;
2. Running status: the start () method has been executed and the running conditions are met, waiting for CPU scheduling;
3. Running status: the instance is running on the CPU;
4. Dead state: the thread stops running, that is, the run () method stops running;
5. Blocking status: the running thread is paused for some reason, such as the CPU scheduling policy, or a process with a higher priority needs to run, or the running conditions are not met.
The following describes how to control threads through sleep and waiting.
1. Sleep
You can call the thread in the run () method of the thread. sleep (long millis) and thread. sleep (long millis, int Nanos) Static Method forces the currently running thread to sleep (pause execution ),
ClassDemo3ExtendsThread { @ Override Public voidRun (){ For(IntI = 5; I <11; I ++ ){ System.Out. Println ("Here the thread sleeps for 5 seconds "); Try{ Thread.Sleep(2000 ); }Catch(Interruptedexception e ){ //TodoAuto-generated Catch Block E. printstacktrace (); } } System.Out. Println ("End of thread running "); } } |
The execution result is to print a sentence every two seconds.
Note:
A. Thread sleep is the best way to help all threads get running opportunities.
B. The thread automatically wakes up after its sleep ends and returns to the running status, not the running status. The time specified in sleep () is the shortest time that the thread does not run. Therefore, the sleep () method cannot guarantee that the thread starts to run after its sleep expires.
C. Sleep () is a static method and can only control the currently running threads.
2. Thread concession yield ()
To understand yield (), you must understand the concept of thread priority. The thread always has a priority. The priority ranges from 1 ~ Between 10. The JVM thread scheduler is a priority-based preemptive scheduling mechanism. In most cases, the priority of the currently running thread is greater than or equal to the priority of any thread in the thread pool. However, this is only the case in most cases.
Note: When designing multi-threaded applications, do not rely on the thread priority. Because the thread scheduling priority operation is not guaranteed, the priority of the thread can only be used as a way to improve program efficiency, but ensure that the program does not rely on this operation.
When all threads in the thread pool have the same priority, the JVM Implementation of the scheduler can freely select the thread it prefers. At this time, there are two possible operations for the Scheduler: one is to select a thread to run until it is blocked or the execution is complete. Second, time slice provides equal operation opportunities for each thread in the pool.
Set the thread priority: the default priority of a thread is the priority of the execution thread that creates it. You can use setpriority (INT newpriority) to change the priority of a thread. For example:
Thread t = new mythread ();
T. setpriority (8 );
T. Start ();
The thread priority is 1 ~ A positive integer between 10. JVM never changes the priority of a thread. However ~ The value between 10 is not guaranteed. Some JVMs may not recognize 10 different values, but merge each of these priorities into less than 10 priorities, then, two or more priority threads may be mapped to one priority.
The default thread priority is 5. There are three constants in the Thread class, which define the thread priority range:
Static int max_priority
The highest priority that a thread can have.
Static int min_priority
The lowest priority that a thread can have.
Static int norm_priority
The default priority assigned to the thread.
The thread. Yield () method is used to pause the currently executed thread object and execute other threads.
What yield () should do is to bring the current running thread back to the runable state, so that other threads with the same priority can get the running opportunity. Therefore, yield () is used to enable proper rotation among threads with the same priority. However, in practice, yield () cannot be guaranteed to achieve the goal of concession, because the concession thread may be re-selected by the thread scheduler.
Conclusion: yield () never leads the thread to the waiting/sleep/blocking status. In most cases, yield () will cause the thread to go from the running state to the runable state, but it may not work.
3. Join () method
The non-static join () method of thread allows a thread B to "join" to the end of another thread. Before Execution of A, B cannot work. For example:
Thread t = new mythread ();
T. Start ();
T. Join ();
In addition, the join () method has an overloaded version with time-out restrictions. For example, T. Join (5000); enables the thread to wait for 5000 milliseconds. If the wait time is exceeded, the thread stops waiting and becomes runable.
PackageLearn. thread; Public classLearnthree { Public static voidMain (string [] ARGs ){ Demo3 demo =NewDemo3 (); For(IntI = 0; I <1000; I ++ ){ System.Out. Println (thread.Currentthread(). Getname () + "this is in the main thread. "); If(5 = I ){ Demo. Start (); Try{ Demo. Join (); }Catch(Interruptedexception e ){ //TodoAuto-generated Catch Block E. printstacktrace (); } } } } } ClassDemo3ExtendsThread { @ Override Public voidRun (){ For(IntI = 5; I <11; I ++ ){ System.Out. Println ("Here the thread sleeps for 5 seconds "); // Thread. Sleep (2000 ); Thread.Yield(); } System.Out. Println ("End of thread running "); } } |
Reference: http://lavasoft.blog.51cto.com/62575/99153