java线程池多线程优先级

Priority of a thread describes how early it gets execution and selected by the thread scheduler. In Java, when we create a thread, always a priority is assigned to it. In a Multithreading environment, the processor assigns a priority to a thread scheduler. The priority is given by the JVM or by the programmer itself explicitly. The range of the priority is between 1 to 10 and there are three constant variables which are static and used to fetch priority of a Thread. They are as following:

线程的优先级描述了线程调度程序提早执行和选择线程的时间。 在Java中,当我们创建线程时,总是为其分配优先级。 在多线程环境中,处理器将优先级分配给线程调度程序。 优先级由JVM或程序员本身明确指定。 优先级的范围在1到10之间,并且有三个常量,它们是静态的,用于获取线程的优先级。 它们如下:

1. public static int MIN_PRIORITY (1. public static int MIN_PRIORITY)

It holds the minimum priority that can be given to a thread. The value for this is 1.

它拥有可以赋予线程的最低优先级。 该值为1。

2. public static int NORM_PRIORITY (2. public static int NORM_PRIORITY)

It is the default priority that is given to a thread if it is not defined. The value for this is 0.

如果未定义,则它是赋予线程的默认优先级。 该值为0。

3. public static int MAX_PRIORITY (3. public static int MAX_PRIORITY)

It is the maximum priority that can be given to a thread. The value for this is 10.

它是可以赋予​​线程的最大优先级。 该值为10。

线程优先级中的Get和Set方法 (Get and Set methods in Thread priority)

1. public final intgetPriority() (1. public final intgetPriority())

In Java, getPriority() method is in java.lang.Thread package. it is used to get the priority of a thread.

在Java中,getPriority()方法位于java.lang.Thread包中。 它用于获取线程的优先级。

2. public final void setPriority(intnewPriority) (2. public final void setPriority(intnewPriority))

In Java setPriority(intnewPriority) method is in java.lang.Thread package. It is used to set the priority of a thread. The setPriority() method throws IllegalArgumentException if the value of new priority is above minimum and maximum limit.

在Java中,setPriority(intnewPriority)方法在java.lang.Thread包中。 它用于设置线程的优先级。 如果新优先级的值高于最小和最大限制,则setPriority()方法将引发IllegalArgumentException。

示例:获取线程优先级 (Example: Fetch Thread Priority)

If we don’t set thread priority of a thread then by default it is set by the JVM. In this example, we are getting thread’s default priority by using the getPriority() method.

如果我们不设置线程的线程优先级,则默认情况下由JVM设置。 在此示例中,我们通过使用getPriority()方法获取线程的默认优先级。

class MyThread extends Thread
{ public void run() { System.out.println("Thread Running..."); } public static void main(String[]args) { MyThread p1 = new MyThread(); MyThread p2 = new MyThread(); MyThread p3 = new MyThread(); p1.start();System.out.println("P1 thread priority : " + p1.getPriority()); System.out.println("P2 thread priority : " + p2.getPriority());  System.out.println("P3 thread priority : " + p3.getPriority()); }
}

P1 thread priority : 5 Thread Running... P2 thread priority : 5 P3 thread priority : 5

P1线程优先级:5线程运行中... P2线程优先级:5 P3线程优先级:5

示例:线程常量 (Example: Thread Constants)

We can fetch priority of a thread by using some predefined constants provided by the Thread class. these constants returns the max, min and normal priority of a thread.

我们可以使用Thread类提供的一些预定义常量来获取线程的优先级。 这些常量返回线程的最大,最小和普通优先级。

class MyThread extends Thread
{ public void run() { System.out.println("Thread Running..."); } public static void main(String[]args) { MyThread p1 = new MyThread(); p1.start();System.out.println("max thread priority : " + p1.MAX_PRIORITY); System.out.println("min thread priority : " + p1.MIN_PRIORITY);  System.out.println("normal thread priority : " + p1.NORM_PRIORITY); }
}

Thread Running... max thread priority : 10 min thread priority : 1 normal thread priority : 5

线程运行中...最大线程优先级:10最小线程优先级:1正常线程优先级:5

示例:设置优先级 (Example : Set Priority)

To set priority of a thread, setPriority() method of thread class is used. It takes an integer argument that must be between 1 and 10. see the below example.

要设置线程的优先级,请使用线程类的setPriority()方法。 它采用必须在1到10之间的整数参数。请参见以下示例。

class MyThread extends Thread
{ public void run() { System.out.println("Thread Running..."); } public static void main(String[]args) { MyThread p1 = new MyThread();// Starting threadp1.start();// Setting priorityp1.setPriority(2);// Getting priorityint p = p1.getPriority();System.out.println("thread priority : " + p);  }
}

thread priority : 2 Thread Running...

线程优先级:2线程正在运行...

例: (Example: )

In this example, we are setting priority of two thread and running them to see the effect of thread priority. Does setting higher priority thread get CPU first. See the below example.

在此示例中,我们设置两个线程的优先级并运行它们以查看线程优先级的效果。 设置更高优先级的线程是否首先获取CPU。 请参见以下示例。

class MyThread extends Thread
{ public void run() { System.out.println("Thread Running... "+Thread.currentThread().getName()); } public static void main(String[]args) { MyThread p1 = new MyThread();MyThread p2 = new MyThread();// Starting threadp1.start();p2.start();// Setting priorityp1.setPriority(2);// Getting -priorityp2.setPriority(1);int p = p1.getPriority();int p22 = p2.getPriority();System.out.println("first thread priority : " + p);  System.out.println("second thread priority : " + p22);}
}

Thread Running... Thread-0 first thread priority : 5 second thread priority : 1 Thread Running... Thread-1

线程运行中...线程-0第一个线程优先级:5第二个线程优先级:1线程运行中...线程-1

Note: Thread priorities cannot guarantee that a higher priority thread will always be executed first than the lower priority thread. The selection of the threads for execution depends upon the thread scheduler which is platform dependent.

注意:线程优先级不能保证高优先级的线程总是比低优先级的线程优先执行。 执行线程的选择取决于平台相关的线程调度程序。

翻译自: https://www.studytonight.com/java/thread-priorities-in-java.php

java线程池多线程优先级

java线程池多线程优先级_Java线程优先级相关推荐

  1. java线程池的应用_Java线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  2. java线程池存在时间_Java线程池基础

    目录: 一.线程池概述 1.线程池类 目前线程池类一般有两个,一个来自于Spring,一个来自于JDK: 来自Spring的线程池:org.springframework.scheduling.con ...

  3. java 线程池 源码_java线程池源码分析

    我们在关闭线程池的时候会使用shutdown()和shutdownNow(),那么问题来了: 这两个方法又什么区别呢? 他们背后的原理是什么呢? 线程池中线程超过了coresize后会怎么操作呢? 为 ...

  4. java 线程池的理解_JAVA线程池原理的理解

    线程池原理基础理解: 线程池初始化规定个数的线程,然后这些线程一直运行,并且监控线程队列,只要线程队列被添加进线程,那么线程池不断从队列中取出线程运行.直到队列中的线程为空.实例代码如下: packa ...

  5. java 线程池 资源回收_JAVA线程池资源回收的问题

    最近项目中为了提高用户体验度,前台创建任务后台任务,用多线程来跑. 现在的场景:后台定时任务管理这两个线程池,一个最大线程数10个,一个最大线程数15.应用部署之后,不超过5个小时,服务器负载高,内存 ...

  6. java线程池 的方法_JAVA线程池的实现方法

    我们大家都知道,在处理多线程服务并发时,由于创建线程需要占用很多的系统资源,所以为了避免这些不必要的损耗,通常我们采用线程池来解决这些问题. 线程池的基本原理是,首先创建并保持一定数量的线程,当需要使 ...

  7. java线程池拒绝策略_Java线程池ThreadPoolExecutor的4种拒绝策略

    最近在做大批量数据采集转换工作,基础数据在本地但是需要调用网络资源完成数据转换.多方面原因在保证良好运行情况下,最多开5个线程进行网络资源调用.方案是基础数据在数据库分页,循环遍历每一条数据,创建调用 ...

  8. java线程池的概念_Java线程池的基本概念以及生命周期

    一.为什么要实现线程池? 线程的创建与销毁对于CPU而言开销较大,通过池化技术可避免重复的创建与销毁线程. 方便与线程资源统一管理. 二.几种常见的线程池以及核心参数 不推荐使用Executor创建线 ...

  9. JAVA中的多线程(八):线程的优先级和yield方法

    JAVA中的多线程(八):线程的优先级和yield方法 优先级代表着抢资源的频率 所有线程默认优先级是5 yield()临时释放线程的执行权 1 class Demo implements Runna ...

最新文章

  1. 改写URL的查询字符串QUERY_STRING(转)
  2. Springboot,SSM框架比较,区别
  3. 数据结构基础-Hash Table详解
  4. [css] 使用css3做一个魔方旋转的效果
  5. 智能硬件开发神器免费送!距离产品智能化,只差一个“三明治”的距离
  6. Vision Transformer中的自监督学习
  7. 3_python基础—运算符 1
  8. 阅读众包文献中一些值得mark 的小收获
  9. 全球以太网交换机和路由器市场:谁领跑?
  10. ServletContext,ActionContext,ServletActionContext
  11. 网上书店系统需求分析说明书
  12. 360极速浏览器代理设置无效问题
  13. 最小二乘法的矩阵推导
  14. html所有标签大全wps文件,html所有标签及其作用说明.wps
  15. 项目使用jdk17人傻了
  16. 个推技术 | 厂商和App必看!统一推送UPS最全解读和快速接入指南
  17. 推荐几款画韦恩图的在线工具
  18. 昨日关注:唐骏说盛大、微软、比尔盖茨及营销
  19. 基于Spring+SpringMVC+Mybatis的Saas开源电商系统
  20. 2022水利水电安全员考试判断题库预测分享(2)

热门文章

  1. 小学奥数题Java编程(2)
  2. Promise学习笔记(上)
  3. MySQL8.0允许远程连接
  4. typora旧版本0.9.41下载地址
  5. 凛冬至,外包咋了,努力照样250!
  6. 数组的flat方法【Array.prototype.flat()】
  7. vue中引入第三方js
  8. 2022年12月29日进行注册身份证功能
  9. NO.26——利用ettercap和driftnet截获数据流里的图片
  10. Element UI 分页问题 this.pageSizes.map is not a function