One, thread scheduling
(1) Adjust the priority of the thread
Java threads have priority, high-priority threads get more execution opportunities, but the opportunity to preempt CPU usage increases, and does not mean that they must be executed first.
The priority of the Java thread is expressed in integers, with a value range of: 1~10, thread having the following 3 static constants:
static int max_priority
The thread can have the highest priority value: 10
static int min_priority
Thread can have the lowest priority, value: 1
static int norm_priority
Default priority assigned to thread, value: 5
The SetPriority () and GetPriority () methods of the thread class are used to set and get the priority of the thread, respectively.
Special Note:
The thread priority is just a description of the high-priority threads competing to the CPU to execute permissions is relatively high, while the low-priority threads compete to the CPU to execute permissions relatively little. But not high-priority threads must first be executed first than the lower-priority thread.
Each thread has a default priority, and the default priority for the main program (main) is: Thread.norm_priority.
The priority of a thread has an inheritance relationship, such as the B thread created in a thread, and the B thread has the same priority as a thread.
The JVM provides 10 priority choices, but it does not map well with the common system, and if you want the program to be well ported to each operating system, you should use only the three static constants in the thread class as the priority, which ensures that the same precedence is used for the same schedule.
(2) Thread hibernation: thread.sleep (Long Millis) method, which causes the thread to go to the blocking state (does not release the held lock on the object). The Millis parameter sets the time to sleep when the sleep is over to the ready (Runnable) state. Sleep () platform portability is good.
(3) Thread waits: The Wait () method in the object class causes the current thread to wait until another thread calls the Notify () or Notifyall () Wake method of this object. These 2 wake-up methods are also methods in the object class, which behaves equivalent to calling wait (0).
(4) Thread concession: the Thread.yield () method pauses the currently executing thread object, giving the execution opportunity to the same or higher priority thread.
(5) Thread join: Join () method, wait for other thread to terminate, call the Join () method of another thread in the current thread, the current thread goes into a blocking state, knows that another thread is executing, and the current thread is again from the blocking state to the ready state.
(6) Thread wake-up: The Notify () method in the object class wakes up a single thread waiting on this objects monitor and, if all threads are waiting on this object, chooses to wake up one of the threads, select arbitrary, and take place when the implementation is determined. The thread waits on the object monitor by calling one of the wait () methods. Until the current thread discards the lock on this object, it can continue to execute the awakened thread. A thread that is awakened will compete with all other threads in the active synchronization on that object in the usual manner, for example: a thread that wakes up does not have a reliable privilege or disadvantage in terms of the next thread that locks this object-fair competition. A similar approach also has a notifyall () that wakes up all the threads waiting on this object monitor.
Special Note:
The suspend () and resume () Two methods in thread are deprecated in JDK1.5 and are no longer introduced because of the deadlock tendency.
Multithreaded Learning-Basic (iii) scheduling of threads