一、线程优先级的继承特性
Java中的线程优先级具有继承特性,如A线程继承了B线程,那么A、B线程优先级一样。
示例:
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:MyThread1
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 上午9:11:15
* 修改人:liuc
* 修改时间:2018年3月19日 上午9:11:15
* 修改备注:
* @version
*
*/
public class MyThread1 extends Thread{
/**
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
System.out.println("MyThread1 run Priority="+this.getPriority());
MyThread2 thread2 = new MyThread2();
thread2.start();
}
}
------------------------------------------------------------------------------------
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:MyThread2
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 上午9:17:32
* 修改人:liuc
* 修改时间:2018年3月19日 上午9:17:32
* 修改备注:
* @version
*
*/
public class MyThread2 extends Thread{
/**
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
System.out.println("MyThread2 run Priority= "+this.getPriority());
}
}
------------------------------------------------------------------------------------
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:Run
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 上午9:20:25
* 修改人:liuc
* 修改时间:2018年3月19日 上午9:20:25
* 修改备注:
* @version
*
*/
public class Run {
public static void main(String[] args) {
System.out.println("Main thread begin priority=" +
Thread.currentThread().getPriority());
// Thread.currentThread().setPriority(6);
System.out.println("Main thread end priority=" +
Thread.currentThread().getPriority());
MyThread1 thread1 = new MyThread1();
thread1.start();
}
}
程序运行结果如下,可见线程初始优先级是一样的。
Main thread begin priority=5
Main thread end priority=5
MyThread1 run priority=5
MyThread2 run priority=5
我们把以下注释代码去掉
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:MyThreadRun
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 上午9:20:25
* 修改人:liuc
* 修改时间:2018年3月19日 上午9:20:25
* 修改备注:
* @version
*
*/
public class MyThreadRun{
public static void main(String[] args) {
System.out.println("Main thread begin priority=" +
Thread.currentThread().getPriority());
Thread.currentThread().setPriority(6);
System.out.println("Main thread end priority=" +
Thread.currentThread().getPriority());
MyThread1 thread1 = new MyThread1();
thread1.start();
}
}
程序运行结果为:
Main thread begin priority=5
Main thread end priority=6
MyThread1 run priority=6
MyThread2 run priority=6
二、优先级具有规则性
上面例子没有看到设置优先级带来的效果,看以下例子:
/**
*
* 项目名称:JavaThread
* 类名称:SystematicnessThread1
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午1:57:49
* 修改人:liuc
* 修改时间:2018年3月19日 下午1:57:49
* 修改备注:
* @version
*
*/
public class SystematicnessThread1 extends Thread{
/**
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int i = 0; i <10; i++) {
for (int j = 0; j < 5000; j++) {
Random random = new Random();
random.nextInt();
addResult = addResult+j;
}
}
long endTime = System.currentTimeMillis();
System.out.println("SystematicnessThread1*** useTime="+(endTime-beginTime));
}
}
------------------------------------------------------------------------------------
package day002;
import java.util.Random;
/**
*
* 项目名称:JavaThread
* 类名称:SystematicnessThread2
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午2:07:16
* 修改人:liuc
* 修改时间:2018年3月19日 下午2:07:16
* 修改备注:
* @version
*
*/
public class SystematicnessThread2 extends Thread{
/**
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int i = 0; i <10; i++) {
for (int j = 0; j < 5000; j++) {
Random random = new Random();
random.nextInt();
addResult = addResult+j;
}
}
long endTime = System.currentTimeMillis();
System.out.println("SystematicnessThread2*** useTime="+(endTime-beginTime));
}
}
------------------------------------------------------------------------------------
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:SystematicnessRun
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午2:08:11
* 修改人:liuc
* 修改时间:2018年3月19日 下午2:08:11
* 修改备注:
* @version
*
*/
public class SystematicnessRun {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
SystematicnessThread1 t1 = new SystematicnessThread1();
t1.setPriority(10);
t1.start();
SystematicnessThread2 t2 = new SystematicnessThread2();
t2.setPriority(1);
t2.start();
}
}
}
程序运行结果如下:可以看到优先级高的SystematicnessThread1基本上都先于低优先级线程SystematicnessThread2完成,但不代表高优先级的线程必定全部先执行完。
SystematicnessThread1*** useTime=22
SystematicnessThread1*** useTime=29
SystematicnessThread1*** useTime=31
SystematicnessThread1*** useTime=19
SystematicnessThread1*** useTime=16
SystematicnessThread1*** useTime=45
SystematicnessThread1*** useTime=16
SystematicnessThread1*** useTime=13
SystematicnessThread1*** useTime=13
SystematicnessThread1*** useTime=13
SystematicnessThread2*** useTime=13
SystematicnessThread2*** useTime=16
SystematicnessThread2*** useTime=26
SystematicnessThread2*** useTime=21
SystematicnessThread2*** useTime=25
SystematicnessThread2*** useTime=28
SystematicnessThread2*** useTime=16
SystematicnessThread2*** useTime=17
SystematicnessThread2*** useTime=18
SystematicnessThread2*** useTime=10
当线程优先级差距很大时,谁先执行完和调用顺序无关。验证如下:
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:SystematicnessRun
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午2:08:11
* 修改人:liuc
* 修改时间:2018年3月19日 下午2:08:11
* 修改备注:
* @version
*
*/
public class SystematicnessRun {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
SystematicnessThread2 t2 = new SystematicnessThread2();
t2.setPriority(1);
t2.start();
SystematicnessThread1 t1 = new SystematicnessThread1();
t1.setPriority(10);
t1.start();
}
}
}
结果为:
SystematicnessThread1*** useTime=93
SystematicnessThread1*** useTime=93
SystematicnessThread1*** useTime=96
SystematicnessThread1*** useTime=97
SystematicnessThread1*** useTime=114
SystematicnessThread1*** useTime=116
SystematicnessThread1*** useTime=116
SystematicnessThread1*** useTime=118
SystematicnessThread1*** useTime=116
SystematicnessThread1*** useTime=116
SystematicnessThread2*** useTime=148
SystematicnessThread2*** useTime=125
SystematicnessThread2*** useTime=18
SystematicnessThread2*** useTime=11
SystematicnessThread2*** useTime=10
SystematicnessThread2*** useTime=9
SystematicnessThread2*** useTime=142
SystematicnessThread2*** useTime=16
SystematicnessThread2*** useTime=15
SystematicnessThread2*** useTime=15
三、线程随机性
一般来说线程的优先级较高则优先执行完run()方法中的任务,但这个结果不是100%成立,因为线程的优先级具有“随机性”,也就是优先级高的线程不一定每一次都先执行完。
看代码:
package day002;
import java.util.Random;
/**
*
* 项目名称:JavaThread
* 类名称:RandomThread1
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午2:27:58
* 修改人:liuc
* 修改时间:2018年3月19日 下午2:27:58
* 修改备注:
* @version
*
*/
public class RandomThread1 extends Thread{
/**
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int i = 0; i <10; i++) {
for (int j = 0; j < 5000; j++) {
Random random = new Random();
random.nextInt();
addResult = addResult+j;
}
}
long endTime = System.currentTimeMillis();
System.out.println("RandomThread1*** useTime="+(endTime-beginTime));
}
}
------------------------------------------------------------------------------------
package day002;
import java.util.Random;
/**
*
* 项目名称:JavaThread
* 类名称:RandomThread2
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午2:28:57
* 修改人:liuc
* 修改时间:2018年3月19日 下午2:28:57
* 修改备注:
* @version
*
*/
public class RandomThread2 extends Thread{
/**
* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int i = 0; i <10; i++) {
for (int j = 0; j < 5000; j++) {
Random random = new Random();
random.nextInt();
addResult = addResult+j;
}
}
long endTime = System.currentTimeMillis();
System.out.println("RandomThread2*** useTime="+(endTime-beginTime));
}
}
------------------------------------------------------------------------------------
package day002;
/**
*
* 项目名称:JavaThread
* 类名称:RandomRun
* 类描述:
* 创建人:liuc
* 创建时间:2018年3月19日 下午2:30:22
* 修改人:liuc
* 修改时间:2018年3月19日 下午2:30:22
* 修改备注:
* @version
*
*/
public class RandomRun {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
RandomThread2 t2 = new RandomThread2();
t2.setPriority(5);
t2.start();
RandomThread1 t1 = new RandomThread1();
t1.setPriority(6);
t1.start();
}
}
}
为了体现随机性将优先级设置为相近,运行多次结果呈现随机性。