一篇文章掌握J(rèn)ava?Thread的類及其常見方法
一,Thread 的幾個常見屬性
Thread 類是 JVM 用來管理線程的一個類,換句話說,每個線程都有一個唯一的 Thread 對象與之關(guān)聯(lián)。
Java中創(chuàng)建線程
顯示繼承Thread,重寫run方法來指定線程執(zhí)行的代碼
匿名內(nèi)部類來繼承Thread,重寫run方法來指定線程執(zhí)行的代碼
顯示實現(xiàn)Runnable接口,重寫run方法
匿名內(nèi)部類來繼承Runnable接口,重寫run方法
通過lambda表達式來描述執(zhí)行的代碼
| 屬性 | 獲取方法 |
| ID | getId() |
| 名稱 | getNmame() |
| 狀態(tài) | getState() |
| 優(yōu)先級 | getPriority() |
| 是否后臺線程 | isDaemon() |
| 是否存活 | isAlive() |
| 是否被中斷 | isInterrupted() |
ID 是線程的唯一標(biāo)識,不同線程不會重復(fù)
名稱是各種調(diào)試工具用到 狀態(tài)表示線程當(dāng)前所處的一個情況,下面我們會進一步說明
優(yōu)先級高的線程理論上來說更容易被調(diào)度到
關(guān)于后臺線程,需要記住一點:JVM會在一個進程的所有非后臺線程結(jié)束后,才會結(jié)束運行。
是否存活,即簡單的理解,為 run 方法是否運行結(jié)束了
線程的中斷問題,下面我們進一步說明
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread("123"){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("線程退出");
}
};
//這一組屬性,線程創(chuàng)建完成后,屬性就不變了
System.out.println(t.getName());
System.out.println(t.getPriority());
System.out.println(t.isDaemon());
System.out.println(t.getId());
//這組屬性會隨著線程的運行而開始改變
System.out.println(t.isAlive());
System.out.println(t.isInterrupted());
System.out.println(t.getState());
t.start();
while (t.isAlive()){
System.out.println("123 正在運行");
System.out.println(t.getState());
System.out.println(t.isInterrupted());
Thread.sleep(300);
}
}

二,線程調(diào)試
1,啟動一個線程
之前我們已經(jīng)看到了如何通過覆寫 run 方法創(chuàng)建一個線程對象,但線程對象被創(chuàng)建出來并不意味著線程就開始運行了。
覆寫 run 方法是提供給線程要做的事情的指令清單
線程對象可以認(rèn)為是把 李四、王五叫過來了
而調(diào)用 start() 方法,就是喊一聲:”行動起來!“,線程才真正獨立去執(zhí)行了。
static class MyThread extends Thread{
@Override
public void run() {
System.out.println("我是一個線程");
}
}
public static void main(String[] args) {
Thread t = new MyThread();
t.start();
}2,中斷一個線程
中斷讓一個程序結(jié)束,結(jié)束可能有兩種情況
1,已經(jīng)把任務(wù)執(zhí)行完了
2,任務(wù)執(zhí)行到一半,被強制結(jié)束
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(){
@Override
public void run() {
while (! isQuit){
System.out.println("正在轉(zhuǎn)賬");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("轉(zhuǎn)賬終止");
}
};
t.start();
Thread.sleep(500);
System.out.println("有內(nèi)鬼,終止交易");
isQuit = true;
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(){
@Override
public void run() {
while (!Thread.interrupted()){
System.out.println("正在轉(zhuǎn)賬");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
System.out.println("轉(zhuǎn)賬終止");
}
};
t.start();
Thread.sleep(5000);
System.out.println("有內(nèi)鬼,終止交易");
t.interrupt();
}
thread 收到通知的方式有兩種:
1. 如果線程因為調(diào)用 wait/join/sleep 等方法而阻塞掛起,則以 InterruptedException 異常的形式通 知,清除中斷標(biāo)志
當(dāng)出現(xiàn) InterruptedException 的時候, 要不要結(jié)束線程取決于 catch 中代碼的寫法. 可以選擇 忽略這個異常, 也可以跳出循環(huán)結(jié)束線程.
2.否則,只是內(nèi)部的一個中斷標(biāo)志被設(shè)置,thread 可以通過
Thread.interrupted() 判斷當(dāng)前線程的中斷標(biāo)志被設(shè)置,清除中斷標(biāo)志
Thread.currentThread().isInterrupted() 判斷指定線程的中斷標(biāo)志被設(shè)置,不清除中斷標(biāo)志
這種方式通知收到的更及時,即使線程正在 sleep 也可以馬上收到。
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println(Thread.interrupted());
}
}
};
t.start();
t.interrupt();
}
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().isInterrupted());
}
}
};
t.start();
t.interrupt();
}
3,等待一個線程
t1與t2串行執(zhí)行
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是線程1");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是線程2");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
System.out.println("主線程執(zhí)行完畢");
}
t1與t2并發(fā)執(zhí)行
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是線程1");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是線程2");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("主線程執(zhí)行完畢");
}
4,休眠線程
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(1000);
System.out.println(System.currentTimeMillis());
}

1,如果線程在正常運行計算判斷邏輯,此時就是在就緒隊列中排隊,調(diào)度器就會從就緒隊列中篩選出合適的PCB讓他在CPU上運行
2,如果某個線程調(diào)用sleep就會讓對應(yīng)的線程的PCB進入阻塞隊列,阻塞隊列無法在PCB上運行
3,時間到了之后,就自動把這個PCB拿回到原來的就緒隊列中
到此這篇關(guān)于一篇文章掌握J(rèn)ava Thread的類及其常見方法的文章就介紹到這了,更多相關(guān)Java Thread內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java TimeoutException:服務(wù)調(diào)用超時異常的正確解決方案
在現(xiàn)代軟件開發(fā)中,服務(wù)間通信是構(gòu)建分布式系統(tǒng)的基礎(chǔ),然而,網(wǎng)絡(luò)延遲、服務(wù)負(fù)載、資源競爭等因素都可能導(dǎo)致服務(wù)調(diào)用超時,TimeoutException是Java中表示服務(wù)調(diào)用超時的常見異常之一,本文將探討TimeoutException的成因及解決方案,需要的朋友可以參考下2024-12-12
詳解eclipse中Maven工程使用Tomcat7以上插件的方法
本篇文章主要介紹了詳解eclipse中Maven工程使用Tomcat7以上插件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
JAVA中@ApiModel和@ApiModelProperty注解實戰(zhàn)代碼
這篇文章主要給大家介紹了關(guān)于JAVA中@ApiModel和@ApiModelProperty注解的相關(guān)資料,@ApiModel注解是用在接口相關(guān)的實體類上的注解,它主要是用來對使用該注解的接口相關(guān)的實體類添加額外的描述信息,常常和@ApiModelProperty注解配合使用,需要的朋友可以參考下2024-03-03
elasticsearch啟動警告無法鎖定JVM內(nèi)存
今天小編就為大家分享一篇關(guān)于elasticsearch啟動警告無法鎖定JVM內(nèi)存,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java純代碼實現(xiàn)導(dǎo)出pdf合并單元格
這篇文章主要為大家詳細介紹了Java如何純代碼實現(xiàn)導(dǎo)出pdf與合并單元格功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
使用IDEA創(chuàng)建Servlet程序的詳細步驟
在學(xué)習(xí)servlet過程中,參考的教程是用eclipse完成的,而我在練習(xí)的過程中是使用IDEA的,在創(chuàng)建servlet程序時遇到了挺多困難,在此記錄一下如何用IDEA完整創(chuàng)建一個servlet程序,感興趣的朋友一起看看吧2024-08-08

