Java?Thread?類和Runnable?接口詳解
一、Thread 類
了解如何使用Thread 類實(shí)現(xiàn)多線程之后,繼續(xù)學(xué)習(xí)Thread 類實(shí)現(xiàn)多線程之后的相關(guān)功能及方法。
1、操作線程名稱的方法
構(gòu)造方法(實(shí)現(xiàn) Runnable 接口時(shí)候使用)
- public Thread(Runnable target,String name); 創(chuàng)建線程時(shí)設(shè)置線程名稱。
成員方法:
- public final void setName(String name); 設(shè)置線程的名稱。
- public final String getName(); 獲取線程的名稱。
Demo 代碼示例:
public class TestThread extends Thread{ @Override public void run() { for (int i = 1; i <= 10; i++) { System.out.println("我正在編寫多線程代碼"+ i); } } //程序主線程 main 線程 public static void main(String[] args) { //創(chuàng)建子類對(duì)象 TestThread thread = new TestThread(); // 設(shè)置線程名稱 thread.setName("姚青新創(chuàng)建的線程"); //調(diào)用 start() 方法開(kāi)啟線程 thread.start(); for (int i = 1; i <= 10; i++) { System.out.println("我正在學(xué)習(xí)多線程"+ i); } // 獲取線程名稱 System.out.println(thread.getName()); } }
運(yùn)行結(jié)果:
2、獲取當(dāng)前正在執(zhí)行的線程
public static Thread currentThread(); 返回當(dāng)前正在執(zhí)行的線程對(duì)象
獲取當(dāng)前線程對(duì)象:Thread.currentThread();
獲取當(dāng)前線程對(duì)象名稱 :Thread.currentThread().getName();
Demo代碼示例:
public class TestThread extends Thread{ @Override public void run() { for (int i = 1; i <= 5; i++) { System.out.println("我正在編寫多線程代碼"+ i); } // 獲取start()方法創(chuàng)建出來(lái)的線程對(duì)象 System.out.println(Thread.currentThread()); // 獲取start()方法創(chuàng)建出來(lái)的線程對(duì)象名稱 System.out.println(Thread.currentThread().getName()); } public static void main(String[] args) { TestThread thread = new TestThread(); thread.setName("姚青新創(chuàng)建的線程"); thread.start(); for (int i = 1; i <= 5; i++) { System.out.println("我正在學(xué)習(xí)多線程"+ i); } //System.out.println(thread.getName()); // 獲取main()主線程對(duì)象名稱 System.out.println(Thread.currentThread().getName()); } }
運(yùn)行結(jié)果:
通過(guò)運(yùn)行結(jié)果可以發(fā)現(xiàn),在控制臺(tái)上分別打印出了 “main”、“Thread[姚青新創(chuàng)建的線程,5,main]”、“姚青新創(chuàng)建的線程”,可以看出將新創(chuàng)建的線程對(duì)象和對(duì)象名稱以及主方法的線程名稱都打印出來(lái)了。
在使用這個(gè)方法的時(shí)候需要注意一點(diǎn),該方法固定的寫法就是 Thread.currentThread(); 放在那個(gè)線程中執(zhí)行這個(gè)方法就是指定的那個(gè)線程。
相關(guān)文章
Java中SimpleDateFormat日期格式轉(zhuǎn)換詳解及代碼示例
這篇文章主要介紹了Java中SimpleDateFormat日期格式轉(zhuǎn)換詳解及代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12eclipse+maven+spring mvc項(xiàng)目基本搭建過(guò)程
這篇文章主要介紹了eclipse+maven+spring mvc項(xiàng)目基本搭建過(guò)程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09springmvc九大組件之HandlerAdapter詳解
這篇文章主要介紹了springmvc九大組件之HandlerAdapter詳解,RequestMappingHandlerAdapter支持的handler的類型是HandlerMethod,而HandlerMethod是通過(guò)解析@RequestMapping注解獲得的,需要的朋友可以參考下2023-11-11Java策略模式的簡(jiǎn)單應(yīng)用實(shí)現(xiàn)方法
這篇文章主要介紹了Java策略模式的簡(jiǎn)單應(yīng)用實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02Java RabbitMQ 中的消息長(zhǎng)期不消費(fèi)會(huì)過(guò)期嗎
RabbitMQ支持消息的過(guò)期時(shí)間,在消息發(fā)送時(shí)可以進(jìn)行指定。 RabbitMQ支持隊(duì)列的過(guò)期時(shí)間,從消息入隊(duì)列開(kāi)始計(jì)算,只要超過(guò)了隊(duì)列的超時(shí)時(shí)間配置,那么消息會(huì)自動(dòng)的清除2021-09-09idea?maven項(xiàng)目啟動(dòng)項(xiàng)目不編譯target?文件的問(wèn)題及解決方法
代碼編輯器中無(wú)編譯錯(cuò)誤,通過(guò)maven 的clean 、compile、package進(jìn)行各種操作也都沒(méi)問(wèn)題,但是單擊綠色箭頭運(yùn)行(默認(rèn)會(huì)先執(zhí)行IDE本身的Build操作)卻報(bào):程序包xxx不存在,這篇文章主要介紹了解決idea maven項(xiàng)目啟動(dòng)項(xiàng)目不編譯target文件問(wèn)題,需要的朋友可以參考下2023-05-05