首页 技术 正文
技术 2022年11月21日
0 收藏 402 点赞 4,496 浏览 1902 个字

概论: 在操作系统中,线程可以划分优先级,优先级高的获得的CPU资源较多,也就是CPU优先执行优先级较高的线程。在JAVA中线程的优先级

分1~~10个10个等级。大于或者小于会报异常。

一、线程优先级具有继承性

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

public class MyThread1 extends Thread {
@Override
public void run() {
System.out.println("MyThread1 run priority=" + this.getPriority());
MyThread2 thread2 = new MyThread2();
thread2.start();
}
}
public class MyThread2 extends Thread {
@Override
public void run() {
System.out.println("MyThread2 run priority=" + this.getPriority());
}
}
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();
}
}

结果:

二、线程优先级具规则性

public class MyThread1 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★thread 1 use time=" + (endTime - beginTime));
}
}
public class MyThread2 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆thread 2 use time=" + (endTime - beginTime));
}
}
public class Run {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread1 thread1 = new MyThread1();
thread1.setPriority(1);
thread1.start(); MyThread2 thread2 = new MyThread2();
thread2.setPriority(10);
thread2.start();
}
}
}

结果:高优先级的线程总是大部分先执行完,运行速度比较快,并不代表全部执行完,线程的优先级与线程的执行顺序无关。出现这样的结果优先级相差10倍造成的。修改为相近的优先级再看看,如下图。

三、线程优先级具具有随机性

多次运行优先级相近的代码,你会发现会有如下结果出现。。可以证实线程的优先级具有随机性。

守护线程(daemon)

概念:任何一个守护线程都是整个JVM中所有非守护线程的“保姆”。。只要当前实例中存在一个非守护线程没有结束,守护线程就在工作。当最后一个非守护线程结束

时,守护线程才会结束工作。(典型,GC垃圾回收器)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848