[Java multithreading] thread priority and guard thread

Mu Xin 2021-11-25 18:30:52
java multithreading thread priority guard

Catalog

1. yield Method

2. Thread priority

3. Inheritance feature of thread priority

4. Priorities are regular

5. The priority is random

6. The guardian thread


1. yield Method

yield() The function of the method is to give up the current CPU resources , Give it up to other tasks to occupy CPU execution time . When the time of giving up is uncertain , Maybe just gave up , Get it right away CPU Time slice .

2. Thread priority

In the operating system , Threads can prioritize , Higher priority threads get CPU More resources , That is to say CPU Priority execution of tasks in higher priority thread objects .

stay Java in , The priority of threads is divided into 1~10 this 10 Level , If it is less than 1 Or greater than 10, be JDK Throw an exception throw new IllegalArgumentException().

JDK Use in 3 Constant to preset the priority value , The code is as follows :

public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;

3. Inheritance feature of thread priority

stay Java in , The priority of a thread is inherited , such as A Thread start B Threads , be B Priority of thread and A It's the same .

4. Priorities are regular

High priority threads always execute most of them first , But it doesn't mean that all high priority threads execute first .

When thread priority levels vary greatly , Who executes first has nothing to do with the calling order of the code .

The priority of thread has certain regularity , That is to say CPU Try to give execution resources to threads with higher priority .

5. The priority is random

If the priority of the thread is higher, the execution of run() Task in method , But the result can't be said for sure , Because the priority of threads also has “ Randomness ”, That is to say, the higher priority thread does not have to execute every time .

Don't measure the priority of threads against the order in which they run results , Higher priority threads do not necessarily complete each execution first run() Task in method , in other words , Thread priority has nothing to do with print order , Don't associate the two . Their relationship is uncertain and random .

6. The guardian thread

stay Java There are two kinds of threads , One is the user thread , The other is guarding (Daemon) Threads .

What is a guardian thread ?

Daemons are special threads , Its characteristics have the meaning of companionship , When there are no non daemons in the process , Then the daemons automatically destroy . A typical daemon thread is a garbage collection thread , When there are no non - daemons in the process , Then there is no need for the garbage collection thread to exist , Automatically destroy .

Use a popular metaphor to explain “ The guardian thread ”: Any one of the daemons is the whole JVM Of all non - daemons in “ nanny ”, As long as the present JVM Any non daemonic thread in the instance is not ended , The daemons are working , Only when the last non - daemonic thread ends , The daemonic thread follows JVM Finish the work together .Daemon Its function is to provide convenient services for other threads , The most typical application of daemons is GC ( Garbage collector ), It is a very competent guardian .

版权声明
本文为[Mu Xin]所创,转载请带上原文链接,感谢
https://javamana.com/2021/11/20211109101849547g.html

  1. Flink Handling Function Real War II: processfunction class, Java thread interview subject
  2. Oracle SQL injection summary
  3. [Java data structure] you must master the classic example of linked list interview (with super detailed illustration and code)
  4. Record a java reference passing problem
  5. spring JTA 關於异常處理的時機問題
  6. Java - Set - Map (double file) - dija Rewriting, 2021 Java Developer's Performance Optimization
  7. Android入门教程 | OkHttp + Retrofit 取消请求的方法
  8. Java 8 Stream API and common methods, Java Junior Program interview
  9. Github 疯传!史上最强!BAT 大佬,2021年最新Java大厂面试笔试题分享
  10. git(3)Git 分支,zookeeper下载教程
  11. Java Backend Internet 500 questions d'entrevue moyennes et avancées (y compris les réponses), technologie de crochet Linux
  12. Entretien d'entretien d'usine Java post sprint de 100 jours - accumulation de jours et de mois, trois questions par jour [jour 12, fonction de principe de Zookeeper
  13. Tutoriel Java - reflection, tutoriel de téléchargement mongodb
  14. How to analyze several common key and hot issues in redis from multiple dimensions
  15. GIT (3) GIT Branch, Zookeeper Download tutoriel
  16. Tutoriel de démarrage Android | okhttp + Retrofit comment annuler une demande
  17. Design pattern [3.3] - Interpretation of cglib dynamic agent source code
  18. Share the actual operation of private collection project nodejs backend + Vue + Mysql to build a management system
  19. Springboot has 44 application initiators
  20. GitHub上霸榜久居不下的《Java面试突击宝典》,java图形用户界面设计基础
  21. GitHub上访问下载破百万的神仙文档《Java面试神技》看完我呆了,java面试问项目中遇到的问题
  22. GitHub上标星75k 超牛的《Java面试突击版,java高级工程师技能
  23. GitHub上标星2,java项目开发实训教程
  24. Docker development environment Preview
  25. JavaScript高級深入淺出:掌握 this 指向
  26. JavaScript Advanced Insight and outside: Mastering this direction
  27. Vue de l'application pratique de Javascript, drop drag Event
  28. docker 安装部署 Jenkins 2.322
  29. kafka安装
  30. 近九万字图文详解RabbitMQ
  31. Engaged in Java for one and a half years, how to break through yourself
  32. 输出9*9乘法表----java
  33. 判断一个数是不是素数-------java
  34. java项目,记录页面修改值,内部打“官司”用
  35. Docker installation Deployment Jenkins 2.322
  36. Comment porter un pantalon en hiver? Les petits hommes, les jambes épaisses et la largeur de l'entrejambe peuvent être vus. 3 techniques pour éviter la foudre
  37. MySQL下载和安装教程
  38. In depth analysis of rocketmq source code - message storage module
  39. Spring transaction management
  40. mysql恢复ibd数据,为何频频报错?
  41. [skills with annual salary of 60W] after working for 5 years, do you really understand netty and why to use it? (deep dry goods)
  42. Pourquoi MySQL récupère - t - il fréquemment les données ibd?
  43. Tutoriels de téléchargement et d'installation MySQL
  44. In IntelliJ idea, develop a fishing and reading plug-in
  45. Talk about how to integrate SPI with spring
  46. 重学Spring系列之Swagger2.0和Swagger3.0
  47. Hadoop05【命令操作】,Java400道面试题通关宝典助你进大厂
  48. Guava Cache缓存设计原理,java基础入门
  49. Gson:GitHub 标星 18K 的 JSON 解析器,Google 出品的 Java JSON 解析器
  50. gRPC学习之六:gRPC-Gateway集成swagger,java语言程序设计与数据结构进阶版
  51. Projet Java, enregistrer la valeur de modification de la page, utilisé pour les litiges internes
  52. Déterminer si un nombre est un nombre premier - - Java
  53. Sortie 9 * 9 tableau de multiplication - Java
  54. Plusieurs façons de réinitialiser le déplacement du Groupe de consommateurs Kafka
  55. GRPC Learning six: GRPC Gateway Integrated Swagger, Java language programming and data structure Advanced Edition
  56. python数据结构:数组、列表、栈、队列及实现
  57. Gson: json Parser for github Star 18k, Java json Parser from Google
  58. Principes de conception du cache Guava, Introduction à la base Java
  59. Un pantalon beige = élégant, blanc + haut de gamme, si beau cet hiver!
  60. Hadoop05 【 commande 】, Java 400 questions d'entrevue pour vous aider à entrer dans l'usine