Java 线程的优先级(setPriority)案例详解

作者:飞少fly 时间:2023-11-12 23:46:39 

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

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

java中优先级分为1-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 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();
}

Java 线程的优先级(setPriority)案例详解

优先级具有规则性


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 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();
}
}

高优先级的线程总是先执行完

Java 线程的优先级(setPriority)案例详解

线程的优先级和代码的执行顺序没有关系

Java 线程的优先级(setPriority)案例详解

优先级具有随机性

一般优先级较高的线程先执行run()方法,但是这个不能说的但肯定,因为线程的优先级具有 “随机性”也就是较高线程不一定每一次都先执行完。


public class MyThread1 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Random random = new Random();
random.nextInt();
}
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();
for (int i = 0; i < 1000; i++) {
Random random = new Random();
random.nextInt();
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆thread 2 use time=" + (endTime - beginTime));
}
}

public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread1 thread1 = new MyThread1();
thread1.setPriority(5);
thread1.start();

MyThread2 thread2 = new MyThread2();
thread2.setPriority(6);
thread2.start();
}
}

Java 线程的优先级(setPriority)案例详解

守护线程介绍:

java 中有两种线程 一个是用户线程,一个是守护(Daemon)线程

典型的守护线程就是垃圾回收线程,如果进程中没有非守护线程了,则守护线程自动销毁。

守护线程作用就是为其他线程的运行提供便利的服务,比如GC。


public class MyThread extends Thread {
private int i = 0;

@Override
public void run() {
try {
while (true) {
i++;
System.out.println("i=" + (i));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.setDaemon(true);//设置守护线程
thread.start();
Thread.sleep(5000);
System.out.println("我离开thread对象也不再打印了,也就是停止了!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Java 线程的优先级(setPriority)案例详解

来源:https://blog.csdn.net/qq_35400008/article/details/80219947

标签:Java,线程
0
投稿

猜你喜欢

  • SpringBoot中shiro过滤器的重写与配置详解

    2021-07-28 23:40:09
  • Java实现的日期处理类完整实例

    2023-12-08 00:16:10
  • Android 文件下载三种基本方式

    2023-02-26 08:21:48
  • Android中AlertDialog四种对话框的最科学编写用法(实例代码)

    2021-09-07 10:25:58
  • Java中String的JdbcTemplate连接SQLServer数据库的方法

    2022-09-05 00:34:12
  • c# 开机启动项的小例子

    2022-11-30 02:16:43
  • Jmeter环境搭建及安装步骤

    2021-11-03 21:06:10
  • Java Map集合用法详解

    2023-08-01 14:47:01
  • Mybatis的特点及优点

    2022-11-19 16:27:54
  • WPF中NameScope的查找规则详解

    2023-01-18 14:22:02
  • android仿音悦台页面交互效果实例代码

    2023-03-27 12:55:54
  • Android播放音乐案例分享

    2023-08-27 14:41:31
  • JAVA 生成随机数并根据后台概率灵活生成的实例代码

    2023-12-05 11:00:48
  • C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信

    2021-10-21 09:08:21
  • 一文详解Jetpack Android新一代导航管理Navigation

    2022-12-08 20:40:14
  • C#实现策略模式

    2022-02-09 17:19:01
  • SpringBoot整合Groovy脚本实现动态编程详解

    2023-04-02 03:24:16
  • 详解Java类库的概念以及import的使用方法

    2022-04-18 06:56:54
  • java 非对称加密算法RSA实现详解

    2023-11-25 07:41:36
  • Java方法调用解析静态分派动态分派执行过程

    2023-05-03 04:32:40
  • asp之家 软件编程 m.aspxhome.com