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

