我是靠谱客的博主 无情汉堡,这篇文章主要介绍JAVA多线程之优先级设置线程特性,现在分享给大家,希望可以做个参考。

JAVA多线程之设置线程的优先级

  • 线程特性
    • setPriority()方法

线程特性

在操作系统中,线程可以划分优先级,优先级较高的线程得到的CPU资源要多一些,也就是CPU优先执行优先级较高的线程对象中的任务,其实就是让高优先级的线程获得更多的CPU时间片,但只是高优先级的线程获取资源的概率会大一点,并不一定保证一定就最先执行,这个取决于CPU

setPriority()方法

此方法在JDK中的源码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/** * Changes the priority of this thread. * <p> * First the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException</code>. * <p> * Otherwise, the priority of this thread is set to the smaller of * the specified <code>newPriority</code> and the maximum permitted * priority of the thread's thread group. * * @param newPriority priority to set this thread to * @exception IllegalArgumentException If the priority is not in the * range <code>MIN_PRIORITY</code> to * <code>MAX_PRIORITY</code>. * @exception SecurityException if the current thread cannot modify * this thread. * @see #getPriority * @see #checkAccess() * @see #getThreadGroup() * @see #MAX_PRIORITY * @see #MIN_PRIORITY * @see ThreadGroup#getMaxPriority() */ public final void setPriority(int newPriority) { ThreadGroup g; checkAccess(); if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } if((g = getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); } }

在这里插入图片描述
可以看到注释:更改线程的优先级有预定义几个优先级MIN_PRIORITY -到MAX_PRIORITY之间
也就是优先级分为1-10个等级,如果不在这个范围内则抛出异常IllegalArgumentException
一下是默认设置的几个级别:1、5、10 1最低,10最高,如果不设置,默认是5

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10;

但线程并不一定会按照这个优先级区执行,只是设置了优先级高的线程获取CUP资源的概率会大一点,但并不能保证百分百就是高优先级的线程最先执行
例如

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void main(String[] arg){ MyThread myThread = new MyThread("A"); myThread.setPriority(1); MyThread myThread2 = new MyThread("B"); myThread2.setPriority(10); MyThread myThread3 = new MyThread("C"); myThread.start(); myThread2.start(); myThread3.start(); } static class MyThread extends Thread{ public MyThread(String name){ super(name); } @Override public void run() { for (int i = 0 ;i<10; i++){ System.out.println(i+"<------------->"+Thread.currentThread().getName()); } } }

结果

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0<------------->B 0<------------->C 1<------------->C 2<------------->C 3<------------->C 0<------------->A 4<------------->C 5<------------->C 6<------------->C 7<------------->C 8<------------->C 9<------------->C 1<------------->B 2<------------->B 3<------------->B 4<------------->B 5<------------->B 1<------------->A 6<------------->B 7<------------->B 8<------------->B 9<------------->B 2<------------->A 3<------------->A 4<------------->A 5<------------->A 6<------------->A 7<------------->A 8<------------->A 9<------------->A

可以从结果看到A,B,C是交互执行,但B和C执行的顺序会在A前面(这个并不是百分百一点的),但这个执行结果C的执行顺序要高于B,再次执行

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0<------------->B 1<------------->B 2<------------->B 3<------------->B 4<------------->B 5<------------->B 6<------------->B 7<------------->B 8<------------->B 9<------------->B 0<------------->C 1<------------->C 2<------------->C 3<------------->C 4<------------->C 5<------------->C 6<------------->C 7<------------->C 8<------------->C 9<------------->C 0<------------->A 1<------------->A 2<------------->A 3<------------->A 4<------------->A 5<------------->A 6<------------->A 7<------------->A 8<------------->A 9<------------->A

可以看到这次的结果和上次有不同
总结:线程启动后纳入到线程调度,线程时刻处于被动获取CPU时间片而无法主动获取。我们可以通过调整线程的优先级来最大程度的干涉线程调度分配时间片的几率,但是能不能抢到资源也是不一定,这个取决于CPU。

大家有问题评论区见!

最后

以上就是无情汉堡最近收集整理的关于JAVA多线程之优先级设置线程特性的全部内容,更多相关JAVA多线程之优先级设置线程特性内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部