Java线程优先级(Priority)

1、线程一

 1 package com.cn.donleo.thread.priority;
 2 
 3 import java.util.concurrent.Callable;
 4 
 5 /**
 6  * @author liangd
 7  * date 2020-10-31 15:25
 8  * code
 9  */
10 public class MyThreadOne extends Thread {
11 
12     MyThreadOne(String name) {
13         super(name);
14     }
15 
16     @Override
17     public void run() {
18         for (int i = 0; i < 20; i++) {
19             System.out.println("第" + i + "个 MyThread: " + getName());
20         }
21         System.out.println("线程1的优先级:" + this.getPriority());
22         MyThreadTwo myThreadTwo = new MyThreadTwo();
23         myThreadTwo.start();
24     }
25 }

 

2、线程二

 1 package com.cn.donleo.thread.priority;
 2 
 3 /**
 4  * @author liangd
 5  * date 2020-10-31 15:20
 6  * code
 7  */
 8 public class MyThreadTwo extends Thread {
 9     @Override
10     public void run() {
11         System.out.println("线程2的优先级:"+this.getPriority());
12     }
13 }

 

3、线程三

 1 package com.cn.donleo.thread.priority;
 2 
 3 
 4 /**
 5  * @author liangd
 6  * date 2020-10-31 17:01
 7  * code
 8  */
 9 public class MyThreadThree extends Thread {
10 
11     MyThreadThree(String name) {
12         super(name);
13     }
14 
15     @Override
16     public void run() {
17         //获取系统当前时间
18         Long start = System.currentTimeMillis();
19         StringBuilder stringBuilder = new StringBuilder();
20         //设置时间间隔
21         MyThreadFour.getTime(stringBuilder);
22         Long end = System.currentTimeMillis();
23         System.out.println("线程3执行时间:" + (end - start));
24     }
25 }

 

4、线程四

 1 package com.cn.donleo.thread.priority;
 2 
 3 import java.util.Random;
 4 
 5 /**
 6  * @author liangd
 7  * date 2020-10-31 17:17
 8  * code
 9  */
10 public class MyThreadFour extends Thread{
11 
12     MyThreadFour(String name) {
13         super(name);
14     }
15 
16     @Override
17     public void run() {
18         Long start = System.currentTimeMillis();
19         StringBuilder stringBuilder = new StringBuilder();
20         getTime(stringBuilder);
21         Long end = System.currentTimeMillis();
22         System.out.println("线程4执行时间:" + (end - start));
23     }
24 
25     /**
26      * 设置时间间隔
27      * @param stringBuilder
28      */
29     static void getTime(StringBuilder stringBuilder) {
30         for (int i = 0; i < 20; i++) {
31             for (int j = 0; j < 50000; j++) {
32                 Random random = new Random();
33                 random.nextInt();
34                 stringBuilder.append(i);
35             }
36         }
37     }
38 }

 

5、测试线程优先级

 1 package com.cn.donleo.thread.priority;
 2 
 3 /**
 4  * @author liangd
 5  * date 2020-10-31 16:49
 6  * code 线程优先级测试
 7  */
 8 public class TestPriority {
 9     /**
10      * 一、优先级
11      *  1、设置优先级 setPriority(8)
12      *  2、得到优先级 getPriority()
13      *  3、线程是有优先级的,也就是线程的执行顺序
14      *     MAX_PRIORITY和MIN_PRIORITY分别是最高级10和最低级1,当然还有默认级别是5;
15      *
16      * 二、线程优先级特性
17      *  1、线程优先级的继承特性:
18      *     也就是如果线程A启动线程B,那么线程A和B的优先级是一样的
19      *  2、线程优先级的规则性:
20      *     即线程会优先级的大小顺序执行,但是不一定是优先级较大的先执行完
21      *  3、线程优先级的随机特性
22      *
23      * @param args
24      */
25     public static void main(String[] args){
26         System.out.println(Thread.currentThread().getPriority());
27         //设置线程优先级,范围0-10,数值越大,优先执行的概率越大
28         Thread.currentThread().setPriority(8);
29         //线程2放在线程1方法里面执行,只需启动线程1
30 //        MyThreadOne threadOne = new MyThreadOne("线程1");
31         //设置线程3的优先级,执行速度概率应该比线程1和线程2更快
32 //        threadOne.start();
33 
34 
35         for (int i=0;i<5 ;i++){
36             MyThreadThree threadThree = new MyThreadThree("线程3");
37             threadThree.setPriority(1);
38             threadThree.start();
39             MyThreadFour threadFour = new MyThreadFour("线程4");
40             threadFour.setPriority(10);
41             threadFour.start();
42         }
43 
44 
45 
46     }
47 }

 

posted @ 2020-12-01 18:12  donleo123  阅读(649)  评论(0编辑  收藏  举报