Java創(chuàng)建線(xiàn)程的五種寫(xiě)法總結(jié)
通過(guò)繼承Thread類(lèi)并實(shí)現(xiàn)run方法創(chuàng)建一個(gè)線(xiàn)程
// 定義一個(gè)Thread類(lèi),相當(dāng)于一個(gè)線(xiàn)程的模板 class MyThread01 extends Thread { // 重寫(xiě)run方法// run方法描述的是線(xiàn)程要執(zhí)行的具體任務(wù)@Overridepublic void run() { System.out.println("hello, thread."); } } // 繼承Thread類(lèi)并重寫(xiě)run方法創(chuàng)建一個(gè)線(xiàn)程 public class Thread_demo01 { public static void main(String[] args) { // 實(shí)例化一個(gè)線(xiàn)程對(duì)象 MyThread01 t = new MyThread01(); // 真正的去申請(qǐng)系統(tǒng)線(xiàn)程,參與CPU調(diào)度 t.start(); } }
通過(guò)實(shí)現(xiàn)Runnable接口,并實(shí)現(xiàn)run方法的方法創(chuàng)建一個(gè)線(xiàn)程
// 創(chuàng)建一個(gè)Runnable的實(shí)現(xiàn)類(lèi),并實(shí)現(xiàn)run方法 // Runnable主要描述的是線(xiàn)程的任務(wù) class MyRunnable01 implements Runnable { @Overridepublic void run() { System.out.println("hello, thread."); } } //通過(guò)繼承Runnable接口并實(shí)現(xiàn)run方法 public class Thread_demo02 { public static void main(String[] args) { // 實(shí)例化Runnable對(duì)象 MyRunnable01 runnable01 = new MyRunnable01(); // 實(shí)例化線(xiàn)程對(duì)象并綁定任務(wù) Thread t = new Thread(runnable01); // 真正的去申請(qǐng)系統(tǒng)線(xiàn)程參與CPU調(diào)度 t.start(); } }
通過(guò)Thread匿名內(nèi)部類(lèi)創(chuàng)建一個(gè)線(xiàn)程
//使用匿名內(nèi)部類(lèi),來(lái)創(chuàng)建Thread 子類(lèi) public class demo2 { public static void main(String[] args) { Thread t=new Thread(){ //創(chuàng)建一個(gè)Thread子類(lèi) 同時(shí)實(shí)例化出一個(gè)對(duì)象 @Override public void run() { while (true){ System.out.println("hello,thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; t.start(); } }
通過(guò)Runnable匿名內(nèi)部類(lèi)創(chuàng)建一個(gè)線(xiàn)程
public class demo3 { //使用匿名內(nèi)部類(lèi) 實(shí)現(xiàn)Runnable接口的方法 public static void main(String[] args) { Thread t=new Thread(new Runnable() { @Override public void run() { while (true){ System.out.println("hello Thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); t.start(); } }
通過(guò)Lambda表達(dá)式的方式創(chuàng)建一個(gè)線(xiàn)程
public class demo4 { //使用 lambda 表達(dá)式 public static void main(String[] args) { Thread t=new Thread(()->{ while (true){ System.out.println("hello,Thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); t.start(); } }
到此這篇關(guān)于Java創(chuàng)建線(xiàn)程的五種寫(xiě)法總結(jié)的文章就介紹到這了,更多相關(guān)Java創(chuàng)建線(xiàn)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springbooot使用google驗(yàn)證碼的功能實(shí)現(xiàn)
這篇文章主要介紹了springbooot使用google驗(yàn)證碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05Java計(jì)算Date類(lèi)時(shí)間差實(shí)例代碼演示
最近工作中遇到需要計(jì)算時(shí)間差,這里給大家總結(jié)下,下面這篇文章主要給大家介紹了關(guān)于Java計(jì)算Date類(lèi)時(shí)間差的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Spring如何基于xml實(shí)現(xiàn)聲明式事務(wù)控制
這篇文章主要介紹了Spring如何基于xml實(shí)現(xiàn)聲明式事務(wù)控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Java編程思想中關(guān)于并發(fā)的總結(jié)
在本文中小編給大家整理的是關(guān)于Java編程思想中關(guān)于并發(fā)的總結(jié)以及相關(guān)實(shí)例內(nèi)容,需要的朋友們參考下。2019-09-09Java實(shí)現(xiàn)郵件發(fā)送QQ郵箱帶附件
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)郵件發(fā)送QQ郵箱帶附件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03springboot集成CAS實(shí)現(xiàn)單點(diǎn)登錄的示例代碼
這篇文章主要介紹了springboot集成CAS實(shí)現(xiàn)單點(diǎn)登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06