The priority of a thread is represented by an integer between 1-10, and the higher the value, the greater the priority, and the default priority is 5.
To open another new thread in one thread, the new threads are called child threads of the thread, and the child threads have the same initial priority as the parent thread.
/**
* java thread: Scheduling of threads-Priority
*
* @author leizhimin
*/
public class Test {
public Stati c void Main (string[] args) {
Thread t1 = new MyThread1 ();
Thread t2 = new Thread (new myrunnable ());
T1.setpriority (10);
T2.setpriority (1);
T2.start ();
T1.start ();
}
}
Class MyThread1 extends Thread {
public void run () {
for int i = 0; I < 10; i++) {
System.out.println ("Thread 1" + i + "secondary execution!") ");
try {
Thread.Sleep;
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
Class Myrunnable implements Runnable {
public void run () {
for (int i = 0; i < i++) {
System.out.println ("Thread 2" + i + "secondary execution!) ");
try {
Thread.Sleep;
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
线程1第0次执行!
线程2第0次执行!
线程2第1次执行!
线程1第1次执行!
线程2第2次执行!
线程1第2次执行!
线程1第3次执行!
线程2第3次执行!
线程2第4次执行!
线程1第4次执行!
线程1第5次执行!
线程2第5次执行!
线程1第6次执行!
线程2第6次执行!
线程1第7次执行!
线程2第7次执行!
线程1第8次执行!
线程2第8次执行!
线程1第9次执行!
线程2第9次执行!
Process finished with exit code 0
Source: http://lavasoft.blog.51cto.com/62575/221801