在多執行緒的情況下,執行緒調度程序根據不同的條件將執行緒指派給特定的行程。 他們的優先事項。 java 執行緒具有預先分配的優先權。除此之外,java虛擬 機器也可以為執行緒分配優先權或由程式設計師明確指定。範圍為 執行緒優先權的值介於 1 到 10(含)之間。三個靜態變數 與優先權相關的有 -
MAX_PRIORITY - 執行緒擁有的最大優先權,預設值為 10。
-
NORM_PRIORITY - 執行緒具有的預設優先級,預設值為 5。
MIN_PRIORITY - 執行緒具有的最小優先權,預設值為 1。
Java 中的「getPriority()」方法有助於傳回綁定為值的執行緒優先權。
「setPriority()」方法會變更給定執行緒的優先權值。它拋出 當執行緒優先權小於 1 或大於 10 時,出現 IllegalArgumentException。
範例
即時示範
import java.lang.*; public class Demo extends Thread{ public void run(){ System.out.println("Now, inside the run method"); } public static void main(String[]args){ Demo my_thr_1 = new Demo(); Demo my_thr_2 = new Demo(); System.out.println("The thread priority of first thread is : " + my_thr_1.getPriority()); System.out.println("The thread priority of first thread is : " + my_thr_2.getPriority()); my_thr_1.setPriority(5); my_thr_2.setPriority(3); System.out.println("The thread priority of first thread is : " + my_thr_1.getPriority()); System.out.println("The thread priority of first thread is : " + my_thr_2.getPriority()); System.out.print(Thread.currentThread().getName()); System.out.println("The thread priority of main thread is : " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(10); System.out.println("The thread priority of main thread is : " + Thread.currentThread().getPriority()); } }
輸出
The thread priority of first thread is : 5 The thread priority of first thread is : 5 The thread priority of first thread is : 5 The thread priority of first thread is : 3 The thread priority of main thread is : 5 The thread priority of main thread is : 10
名為 Demo 的類別繼承自基底類別 Thread。函數‘run’被定義且相關 訊息被定義。在 main 函數中,建立了 Demo 類別的兩個實例,並將它們 優先權是透過呼叫函數“getPriority”找到的。
它們被印在控制台上。接下來,使用以下方法為 Demo 實例指派優先權: ‘設定優先權’函數。輸出顯示在控制台上。列印線程的名稱 使用「getName」功能在螢幕上顯示。
以上是多執行緒中的Java執行緒優先權的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器