线程Priority:

线程可以划分优先级,优先级较高的线程得到的CPU资源较多,也就是CPU优先执行优先级较高的线程对象中的任务。

设置线程优先级有助于帮助“线程规划器”确定在下一次选择哪个线程来优先执行。

线程优先级分为10个等级,1-》10

三个常用等级:

线程优先级继承性:

A线程启动了B线程,则B线程的优先级与A线程是一样的。

public class PriorityInheritThread extends Thread {
@Override
public void run() {
System.out.println("1 Run priority = " + this.getPriority());
PriorityInheritThread2 thread2 = new PriorityInheritThread2();
thread2.start();
}
} public class PriorityInheritThread2 extends Thread{
@Override
public void run() {
System.out.println("2 Run priority = " + this.getPriority());
}
}
public class ThreadRunMain {
public static void main(String[] args) {
testPriorityInheritThread();
} public static void testPriorityInheritThread() {
System.out.println("Main thread begin priority = " + Thread.currentThread().getPriority());
Thread.currentThread().setPriority(6);
System.out.println("Main thread end priority = " + Thread.currentThread().getPriority());
PriorityInheritThread thread1 = new PriorityInheritThread();
thread1.start();
}
}

运行结果:

线程规则性:

优先级高的线程总是大部分先执行完。

线程随机性:

当线程优先等级差距很大,谁先执行完和Main线程代码的调用顺序无关。优先级高的线程也不一定每一次都先执行完。

public class RunFastThreadA extends Thread {
private int count = 0; public int getCount() {
return count;
} @Override
public void run() {
while(true){
count ++;
}
}
} public class RunFastThreadB extends Thread {
private int count = 0; public int getCount() {
return count;
} @Override
public void run() {
while(true){
count ++;
}
}
} public class ThreadRunMain {
public static void main(String[] args) {
testRunFastThread();
} public static void testRunFastThread() {
try {
RunFastThreadA a = new RunFastThreadA();
a.setPriority(Thread.NORM_PRIORITY - 3);
a.start();
RunFastThreadB b = new RunFastThreadB();
b.setPriority(Thread.NORM_PRIORITY + 3);
b.start();
Thread.sleep(10000);
a.stop();
b.stop();
System.out.println("a = " + a.getCount());
System.out.println("b = " + b.getCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

运行结果:

Java 学习笔记之 线程Priority的更多相关文章

  1. java学习笔记15--多线程编程基础2

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...

  2. java学习笔记14--多线程编程基础1

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...

  3. 0040 Java学习笔记-多线程-线程run()方法中的异常

    run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...

  4. 0039 Java学习笔记-多线程-线程控制、线程组

    join线程 假如A线程要B线程去完成一项任务,在B线程完成返回之前,不进行下一步执行,那么就可以调用B线程的join()方法 join()方法的重载: join():等待不限时间 join(long ...

  5. java学习笔记之线程(Thread)

    刚开始接触java多线程的时候,我觉得,应该像其他章节的内容一样,了解了生命周期.构造方法.方法.属性.使用的条件,就可以结束了,然而随着我的深入学习了解,我发现java的多线程是java的一个特别重 ...

  6. 0041 Java学习笔记-多线程-线程池、ForkJoinPool、ThreadLocal

    什么是线程池 创建线程,因为涉及到跟操作系统交互,比较耗费资源.如果要创建大量的线程,而每个线程的生存期又很短,这时候就应该使用线程池了,就像数据库的连接池一样,预先开启一定数量的线程,有任务了就将任 ...

  7. 【java学习笔记】线程

    1.线程的定义 ①继承Thread类,将执行的任务逻辑放到run方法中,调用start方法来开启线程 public class ThreadDemo { public static void main ...

  8. Java学习笔记之——线程的生命周期、线程同步

    一. 线程的生命周期 新建(new Thrad):创建线程后,可以设置各个属性值,即启动前 设置 就绪(Runnable):已经启动,等待CPU调动 运行(Running):正在被CPU调度 阻塞(B ...

  9. JAVA学习笔记16——线程生命周期

    当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态,在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞(Blocking)和 ...

随机推荐

  1. 牛客国庆集训派对Day6 A Birthday 费用流

    牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...

  2. HDU3068 最长回文 Manacher's Algorithm 马拉车算法 模板

    HDU3068 复习了一下这个算法, 注意数组大小要开两倍大. #include <algorithm> #include <iterator> #include <io ...

  3. codeforces 389 D. Fox and Minimal path(构造+思维)

    题目链接:https://vjudge.net/contest/175446#problem/J 题解:显然要用最多n个点构成的图要使的得到的最短路条数有1e9次个,显然要有几个数相乘容易想到2的几进 ...

  4. Light It Up CF1000B 思维

    Light It Up time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. Pytorch读取,加载图像数据(一)

    在学习Pytorch的时候,先学会如何正确创建或者加载数据,至关重要. 有了数据,很多函数,操作的效果就变得很直观. 本文主要用其他库读取图像文件(学会这个,你就可以在之后的学习中,将一些效果直观化) ...

  6. 解决flutter:unable to find valid certification path to requested target 的问题

    1.问题 周末在家想搞搞flutter,家里电脑是windows的,按照官网教程一步步安装好以后,创建flutter工程,点击运行,一片红色弹出来,WTF? PKIX path building fa ...

  7. Codeforces1093E_Intersection of Permutations

    题意 给定两个排列a和b,两种操作,交换b_i和b_j,询问a[l_a...r_a]和b[l_b...r_b]有多少个数相同. 分析 由于给的是排列,保证b的每个数都有a的对应,构造数组c,c[i]表 ...

  8. 014 Python基本图形绘制小结

    目录 一.Python基本语法元素 1.1 温度转换 二.Python基本图形绘制 2.1 Python蟒蛇绘制 一.Python基本语法元素 缩进.注释.命名.变量.保留字 数据类型.字符串. 整数 ...

  9. android中fragment与activity之间通信原理以及例子

    参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...

  10. 【赶快收藏】Hystrix实战,优雅提升系统的鲁棒性

    背景 最近接手了一个系统,其功能都是查询.查询分了两种方式,一种是公司集团提供的查询能力,支持全国各个省份的查询,但是业务高峰期时服务响应比较慢:另外一种是各省的分公司都分别提供了对应的查询能力,但是 ...