Thread類的基本用法(上)

一、線程創建方法(5種)

1.繼承Thread

class MyThread extends Thread {@Overridepublic void run() {System.out.println("MyThread is running");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

這里中間運行的代碼,需要拋出異常。

然后創建MyThread的實例,并執行

public class Demo1 {public static void main(String[] args) throws InterruptedException {Thread t = new MyThread();t.start();//t.run();while(true){System.out.println("hello world");Thread.sleep(1000);}}
}

2.實現Runnable接口

class MyRunnable implements Runnable {@Overridepublic void run() {while (true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

然后創建Thread實例,將Runnable對象作為參數。

public class Demon2 {public static void main(String[] args) throws InterruptedException {Runnable runnable = new MyRunnable();Thread thread = new Thread(runnable);thread.start();while(true) {System.out.println("hello main");Thread.sleep(1000);}}
}

這里也可以寫成

public class Demon2 {public static void main(String[] args) throws InterruptedException {
/*        Runnable runnable = new MyRunnable();Thread thread = new Thread(runnable);*/Thread thread = new Thread(new MyRunnable());thread.start();while(true) {System.out.println("hello main");Thread.sleep(1000);}}
}

后面的使用方法不變。

3.使用匿名內部類創建Thread子類對象

public class Demon2 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(){@Overridepublic void run() {while(true) {System.out.println("hello world");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};t.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}

實際上就是第一種方法的變形。

4.匿名內部類創建Runnable子類對象

public class Demon3 {public static void main(String[] args) throws InterruptedException {Runnable runnable = new Runnable() {@Overridepublic void run() {while(true) {System.out.println("hello world");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};Thread thread = new Thread(runnable);thread.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}

后面依舊需要創建Thread實例化,將Runnable作為對象參數傳入。

5.使用lambda表達式創建子類

public class Demon4 {public static void main(String[] args) throws InterruptedException {Thread t  = new Thread(()->{while(true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}

這也是最簡單最常用的方法。

二、線程中斷

這里采用Thread中的interrupted方法

public class Demon10 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while(!Thread.currentThread().isInterrupted()){//System.out.println("t:"+Thread.currentThread().getName());System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {//throw new RuntimeException(e);break;}}System.out.println("t結束");});t.start();//System.out.println("main:"+Thread.currentThread().getName());Thread.sleep(3000);System.out.println("main嘗試終止");t.interrupt();}
}

t.interrupt在調用后內部類中的while循環條件被打破,直接打印“t結束”。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/98644.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/98644.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/98644.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ARM內存映射與啟動地址重映射機制解析

目錄 內存映射 1. 核心概念:內存映射 (Memory Map) 2. 啟動過程與地址重映射 (Remapping) 關鍵:啟動引腳 (Boot Pins) 這個過程可以類比: 3. 為什么設計成這樣? 4. 一圖流總結 圖解說明: 核心要點:…

網絡原理——傳輸層協議TCP基本認識

文章目錄傳輸層協議TCP基本認識TCP協議的格式TCP的可靠性初步理解——確認應答機制暫時理解TCP的通信過程TCP的確認號和確認序號確認號和確認序號的意義捎帶應答TCP中其他字段的理解16位窗口大小標志位標志位的本質標志位的意義以SYN ACK標志位簡單理解TCP連接三次握手以FIN標…

Java HTTP響應的流式處理技術

第1章 引言 1.1 傳統HTTP響應處理的局限性 在現代Web應用開發中,HTTP通信是系統間數據交換的核心方式。隨著數據量的不斷增長和實時性要求的提高,傳統的HTTP響應處理方式逐漸暴露出諸多問題。 傳統處理方式通常需要將整個HTTP響應體一次性加載到內存中,然后再進行處理。這…

D01-【計算機二級】Python(1)基本操作第41題

1、考生文件夾下存在一個文件 PY101.py,請寫代碼替換橫線,不修改其他代碼,實現以下功能: 鍵盤輸入正整數 n,按要求把 n 輸出到屏幕,格式要求:寬度為 20 個字符,減號字符 - 填充,右對…

工程師 - Onion Architecture in Software Development

Introduction 介紹 In the ever-evolving world of software development, finding the right architectural pattern is akin to selecting the foundation for a building. One such architectural paradigm that has gained recognition for its ability to promote mainta…

TightVNC功能介紹

TightVNC是一款跨平臺的遠程桌面工具,支持Windows、Linux等系統,通過高效壓縮技術實現低帶寬環境下的流暢控制。以下是詳細的使用說明: 一、安裝與配置 1. Windows系統 下載與安裝 訪問TightVNC官網下載安裝包,運行后選擇“Comp…

硬件 (七) ARM 軟中斷, IMX6ULL 點燈

一、ARM 軟中斷(SVC):從用戶態到內核態的橋梁軟中斷(SVC,Supervisor Call)是 ARM 處理器從 “非特權模式(如 User)” 進入 “特權模式(如 Supervisor)” 的核…

數據結構與算法-樹和二叉樹-二叉樹的存儲結構(Binary Tree)

樹和二叉樹的內容比較多,分成兩次來發 4 樹和二叉樹(Tree and Binary Tree) 4.1 樹和二叉樹的定義 4.1.1 樹的定義 樹(Tree)是 n(n>0)個結點的有限集,它或為空樹(…

CentOS7 Hive2.3.8 安裝圖文教程

一、 安裝MySQL 0.0)查詢mariadb,有就去0.1),沒有就不管直接去1) rpm -qa | grep mariadb0.1)卸載mariadb rpm -e --nodeps 查詢出來的內容二、安裝MySQL 1.下載資源包 官網下載 MySQL官網下載地址:https://dev.m…

開發避坑指南(43):idea2025.1.3版本啟動springboot服務輸入jvm參數解決辦法

問題 最近裝了新版IDEA嘗嘗新特性,IntelliJ IDEA 2025.1.3,可是在運行springboot服務的時候,找了好久才找到輸入jvm啟動參數的地方。如上圖,不像舊版的IDEA,在Run/Debug Configurations->Configuration->Environ…

《sklearn機器學習——數據預處理》標準化或均值去除和方差縮放

數據集的標準化是scikit-learn中實現許多機器學習估計器的普遍要求;如果個別特征看起來或多或少不像標準正態分布數據:均值和單位方差為零的高斯分布,則它們的性能可能不好。 在實踐中,我們通常會忽略分布的形狀,而只是…

leedcode 算法刷題第三十一天

1049. 最后一塊石頭的重量 II 有一堆石頭&#xff0c;用整數數組 stones 表示。其中 stones[i] 表示第 i 塊石頭的重量。 每一回合&#xff0c;從中選出任意兩塊石頭&#xff0c;然后將它們一起粉碎。假設石頭的重量分別為 x 和 y&#xff0c;且 x < y。那么粉碎的可能結果…

圖神經網絡介紹

源自論文&#xff1a;Survey on Graph Neural Networks 圖神經網絡&#xff08;GNNs&#xff09;中的符號與定義詳解 本文使用了圖論和深度學習領域的標準符號體系&#xff0c;以確保對圖結構數據的描述清晰一致。以下是核心符號和定義的詳細說明&#xff1a; 一、基礎圖結構符…

測試報告:“問卷考試系統”項目

目錄 一、報告概述 &#xff08;一&#xff09;項目背景 &#xff08;二&#xff09;項目核心模塊與測試目的 1、項目核心模塊 2、測試目的 &#xff08;三&#xff09;測試環境 1、硬件環境 2、軟件環境 &#xff08;1&#xff09;操作系統 &#xff08;2&#xff0…

Linux筆記---網絡計算器

1. 網絡程序分層 我們說過&#xff0c;OSI7層模型十分完美&#xff0c;但是因特網實際上采用的是TCP/IP五層模型&#xff1a; 實際上&#xff0c;對比可以發現&#xff0c;TCP/IP模型實際上就是將OSI的前三層模型合并為了應用層。 這就提示我們&#xff0c;我們設計的應用程…

《智能網聯汽車交通仿真軟件可信度評估》團標啟動會圓滿舉辦

讓數據真正閉環的L4級自動駕駛仿真工具鏈&#xff0d;杭州千岑智能科技有限公司&#xff1a;RSim 近日&#xff0c;由中國仿真學會主辦、清華大學牽頭的《智能網聯汽車交通仿真軟件可信度評估》團體標準啟動會在北京成功舉行。杭州千岑科技有限公司作為智能網聯汽車測試驗證領域…

關于 MCU 芯片外圍電路的快速入門介紹

MCU&#xff08;微控制單元&#xff0c;Microcontroller Unit&#xff09;是嵌入式系統的“大腦”&#xff0c;但需通過外圍電路實現供電、信號輸入/ 輸出、通信、存儲等功能&#xff0c;才能構成完整的工作系統。外圍電路的設計直接決定 MCU 的穩定性、功能擴展性和適用場景&a…

Uniapp onLoad 和 onShow 區別

一、核心區別生命周期觸發時機執行次數參數獲取onLoad頁面首次創建時觸發僅1次支持獲取URL參數optionsonShow頁面每次顯示時觸發&#xff08;包括返回&#xff09;多次無法獲取URL參數二、實戰數據請求場景優先使用onLoad請求數據的場景&#xff1a;初始化數據當需要根據URL參數…

大模型預訓練評估指標

模型效果評測 關于 Language Modeling 的量化指標&#xff0c;較為普遍的有 [PPL]&#xff0c;[BPC]等,可以簡單理解為在生成結果和目標文本之間的 Cross Entropy Loss 上做了一些處理&#xff0c;這種方式可以用來評估模型對「語言模板」的擬合程度即給定一段話&#xff0c;預…

【Matlab】-- 機器學習項目 - 基于XGBoost算法的數據回歸預測

文章目錄 文章目錄01 內容概要02 部分代碼03 代碼解讀04 運行結果05 基于XGBoost算法的數據回歸預測源碼01 內容概要 XGBoost屬于集成學習中的Boosting方法&#xff0c;其基本思想是&#xff1a; 逐步構建多個弱學習器&#xff08;通常是CART決策樹&#xff09;&#xff0c;每…