Thread Priority
The priority of the thread passes the importance of the thread to the thread scheduler, which tends to allow the highest priority thread to execute first. Then this does not mean that the thread with the lower priority value will not be executed. A less-prioritized thread is only less frequent
Package com.yin.myproject.demo.concurrent.base;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
public class Simplepriorities implements Runnable {private int countdown = 5;
private volatile double D;
private int priority;
public simplepriorities (int priority) {this.priority = priority; Public String toString () {return "name:" + Thread.CurrentThread (). GetName () + "priority:" + Thread.curre
Ntthread (). GetPriority () + ":p rority:" + ":" + countdown;
public void Run () {Thread.CurrentThread (). SetPriority (priority);
while (true) {for (int i = 1; i < 10000 i++) {D + = (Math.PI + MATH.E)/(double) I;
if (i% 1000 = = 0) {Thread.yield ();
} System.out.println (this);
if (--countdown = = 0) return; }} public static void Main (string[] args) {Executorservice exec = Executors.newcachedthreadpool ();
for (int i = 0; i < 5; i++) {Exec.execute (new simplepriorities (thread.min_priority));
Exec.execute (New Simplepriorities (thread.max_priority));
} exec.shutdown ();
}
}
Output results (each result will be biased):
Name:pool-1-thread-6 priority:10:5
name:pool-1-thread-3 priority:1:5
name:pool-1-thread-4 priority:1:5
name:pool-1-thread-1 priority:1:5
name:pool-1-thread-5 priority:1:5
name:pool-1-thread-2 priority:1: 5
name:pool-1-thread-1 priority:1:4
name:pool-1-thread-6 priority:10:4
name:pool-1-thread-4 Priority: 1:4
name:pool-1-thread-3 priority:1:4
...
From the execution result we can sum up to two points:
1. Thread priority does not guarantee the thread's execution order, the priority is not necessarily performed first, and the low priority is not necessarily performed
2. Do not attempt to use precedence to control the execution order of threads yield () function
First look at the official document explanation:
A hint to the scheduler, the current thread is willing to yield it current use of a processor. The scheduler is free to ignore this hint.
The general meaning is that when the Thread.yield () function is invoked, it gives the thread scheduler a hint that the current thread is willing to give up the CPU, but the thread scheduler may ignore the hint. The popular explanation is that the current thread is willing to give up the right to use the CPU to allow other threads to continue executing, but the thread scheduler may stop the current thread from continuing, or it may allow the thread to continue executing.
The following is an example to illustrate the use of the Thread.yield () function:
public class YieldDemo01 {public
static void Main (string[] args) {
Yieldthread t1 = new Yieldthread ("Thread One");
Yieldthread t2 = new Yieldthread ("Thread Two");
T1.start ();
T2.start ();
}
Class Yieldthread extends Thread {
private String name;
Public Yieldthread (String name) {
this.name = name;
}
@Override public
Void Run () {A for
(int i = 1; I <= i++) {
System.out.println (name + i);
if (i = = 5) {
Thread.yield ()
;
}
}}}
The results of each run may be biased, but note the execution results after thread 15 and thread 25.
Another understanding: There is another understanding on the web that when the Thread.yield () function is invoked, the current thread is stopped, and then the other line Cheng the control of the CPU, so it is possible that the current thread is executing, or other threads may be executed. According to the official documentation and thinking in Java, this understanding is a mistake. When you call Thread.yiele (), there is no mechanism to ensure that the current thread is bound to be stopped. Summary
As you can see, both the priority and yield () functions attempt to control the sequential execution of threads. But we need to be aware that there are no mechanisms in Java that guarantee the order in which threads are executed, and that the two practices simply increase the chances of switching between threads.