欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章掌握J(rèn)ava?Thread的類及其常見(jiàn)方法

 更新時(shí)間:2022年03月02日 14:56:01   作者:/少司命  
Thread類用于操作線程,是所以涉及到線程操作(如并發(fā))的基礎(chǔ)。本文將通過(guò)代碼對(duì)Thread類的功能作用及其常見(jiàn)方法進(jìn)行分析

一,Thread 的幾個(gè)常見(jiàn)屬性

Thread 類是 JVM 用來(lái)管理線程的一個(gè)類,換句話說(shuō),每個(gè)線程都有一個(gè)唯一的 Thread 對(duì)象與之關(guān)聯(lián)。

Java中創(chuàng)建線程

顯示繼承Thread,重寫(xiě)run方法來(lái)指定線程執(zhí)行的代碼

匿名內(nèi)部類來(lái)繼承Thread,重寫(xiě)run方法來(lái)指定線程執(zhí)行的代碼

顯示實(shí)現(xiàn)Runnable接口,重寫(xiě)run方法

匿名內(nèi)部類來(lái)繼承Runnable接口,重寫(xiě)run方法

通過(guò)lambda表達(dá)式來(lái)描述執(zhí)行的代碼

屬性獲取方法
IDgetId()
名稱getNmame()
狀態(tài)getState()
優(yōu)先級(jí)getPriority()
是否后臺(tái)線程isDaemon()
是否存活isAlive()
是否被中斷isInterrupted()

ID 是線程的唯一標(biāo)識(shí),不同線程不會(huì)重復(fù)

名稱是各種調(diào)試工具用到 狀態(tài)表示線程當(dāng)前所處的一個(gè)情況,下面我們會(huì)進(jìn)一步說(shuō)明

優(yōu)先級(jí)高的線程理論上來(lái)說(shuō)更容易被調(diào)度到

關(guān)于后臺(tái)線程,需要記住一點(diǎn):JVM會(huì)在一個(gè)進(jìn)程的所有非后臺(tái)線程結(jié)束后,才會(huì)結(jié)束運(yùn)行。

是否存活,即簡(jiǎn)單的理解,為 run 方法是否運(yùn)行結(jié)束了

線程的中斷問(wèn)題,下面我們進(jìn)一步說(shuō)明

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());
        //這組屬性會(huì)隨著線程的運(yùn)行而開(kāi)始改變
        System.out.println(t.isAlive());
        System.out.println(t.isInterrupted());
        System.out.println(t.getState());
 
        t.start();
 
        while (t.isAlive()){
            System.out.println("123 正在運(yùn)行");
            System.out.println(t.getState());
            System.out.println(t.isInterrupted());
            Thread.sleep(300);
        }
    }

二,線程調(diào)試

1,啟動(dòng)一個(gè)線程

之前我們已經(jīng)看到了如何通過(guò)覆寫(xiě) run 方法創(chuàng)建一個(gè)線程對(duì)象,但線程對(duì)象被創(chuàng)建出來(lái)并不意味著線程就開(kāi)始運(yùn)行了。

覆寫(xiě) run 方法是提供給線程要做的事情的指令清單

線程對(duì)象可以認(rèn)為是把 李四、王五叫過(guò)來(lái)了

而調(diào)用 start() 方法,就是喊一聲:”行動(dòng)起來(lái)!“,線程才真正獨(dú)立去執(zhí)行了。

 static class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("我是一個(gè)線程");
        }
    }
 
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
    }

2,中斷一個(gè)線程

中斷讓一個(gè)程序結(jié)束,結(jié)束可能有兩種情況

        1,已經(jīng)把任務(wù)執(zhí)行完了

        2,任務(wù)執(zhí)行到一半,被強(qiáng)制結(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. 如果線程因?yàn)檎{(diào)用 wait/join/sleep 等方法而阻塞掛起,則以 InterruptedException 異常的形式通 知,清除中斷標(biāo)志

        當(dāng)出現(xiàn) InterruptedException 的時(shí)候, 要不要結(jié)束線程取決于 catch 中代碼的寫(xiě)法. 可以選擇 忽略這個(gè)異常, 也可以跳出循環(huán)結(jié)束線程.

2.否則,只是內(nèi)部的一個(gè)中斷標(biāo)志被設(shè)置,thread 可以通過(guò)

        Thread.interrupted() 判斷當(dāng)前線程的中斷標(biāo)志被設(shè)置,清除中斷標(biāo)志

        Thread.currentThread().isInterrupted() 判斷指定線程的中斷標(biāo)志被設(shè)置,不清除中斷標(biāo)志

這種方式通知收到的更及時(shí),即使線程正在 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,等待一個(gè)線程

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,如果線程在正常運(yùn)行計(jì)算判斷邏輯,此時(shí)就是在就緒隊(duì)列中排隊(duì),調(diào)度器就會(huì)從就緒隊(duì)列中篩選出合適的PCB讓他在CPU上運(yùn)行

2,如果某個(gè)線程調(diào)用sleep就會(huì)讓對(duì)應(yīng)的線程的PCB進(jìn)入阻塞隊(duì)列,阻塞隊(duì)列無(wú)法在PCB上運(yùn)行

3,時(shí)間到了之后,就自動(dòng)把這個(gè)PCB拿回到原來(lái)的就緒隊(duì)列中

到此這篇關(guān)于一篇文章掌握J(rèn)ava Thread的類及其常見(jiàn)方法的文章就介紹到這了,更多相關(guān)Java Thread內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis 使用權(quán)威指南

    MyBatis 使用權(quán)威指南

    MyBatis 是支持定制化 SQL、存儲(chǔ)過(guò)程以及高級(jí)映射的優(yōu)秀的持久層框架。MyBatis 避免了幾乎所有的 JDBC 代碼和手動(dòng)設(shè)置參數(shù)以及獲取結(jié)果集,本文給大家介紹MyBatis 使用指南,一起看看吧
    2017-03-03
  • 解決Spring Security 用戶帳號(hào)已被鎖定問(wèn)題

    解決Spring Security 用戶帳號(hào)已被鎖定問(wèn)題

    這篇文章主要介紹了解決Spring Security 用戶帳號(hào)已被鎖定問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 解決使用mybatis取值,字段賦值錯(cuò)誤的問(wèn)題

    解決使用mybatis取值,字段賦值錯(cuò)誤的問(wèn)題

    這篇文章主要介紹了解決使用mybatis取值,字段賦值錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • IDEA2023.1.3安裝教程及下載(圖文)

    IDEA2023.1.3安裝教程及下載(圖文)

    最新變化是在IDEA?2023.1中,對(duì)新UI做出了大量改進(jìn),本文主要介紹了IDEA2023.1.3安裝教程及下載,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • SpringBoot如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證

    SpringBoot如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證

    為了保證參數(shù)的正確性,我們需要使用參數(shù)驗(yàn)證機(jī)制,來(lái)檢測(cè)并處理傳入的參數(shù)格式是否符合規(guī)范,所以本文就來(lái)和大家聊聊如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證吧
    2023-08-08
  • 解讀System.getProperty("ENM_HOME")中的值從哪獲取的

    解讀System.getProperty("ENM_HOME")中的值從哪獲取的

    這篇文章主要介紹了解讀System.getProperty("ENM_HOME")中的值從哪獲取的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • JVM的垃圾回收機(jī)制你了解嗎

    JVM的垃圾回收機(jī)制你了解嗎

    這篇文章主要為大家介紹了JVM的垃圾回收機(jī)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Java中關(guān)于String StringBuffer StringBuilder特性深度解析

    Java中關(guān)于String StringBuffer StringBuilder特性深度解析

    這篇文章主要介紹了Java中關(guān)于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Java Thread中start()和run()的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java Thread中start()和run()的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    start() : 它的作用是啟動(dòng)一個(gè)新線程,新線程會(huì)執(zhí)行相應(yīng)的run()方法。start()不能被重復(fù)調(diào)用。而run() : run()就和普通的成員方法一樣,可以被重復(fù)調(diào)用。下面通過(guò)示例代碼給大家介紹了Java Thread中start()和run()的區(qū)別,感興趣的朋友一起看看吧
    2017-05-05
  • Spring Bean的線程安全問(wèn)題

    Spring Bean的線程安全問(wèn)題

    Spring容器中的Bean是否線程安全,本文主要介紹了Spring Bean的線程安全問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06

最新評(píng)論