Java創(chuàng)建線程的五種寫法總結(jié)
更新時(shí)間:2022年08月19日 10:39:16 作者:學(xué)好c語言的小王同學(xué)
本文主要為大家詳細(xì)介紹一下Java實(shí)現(xiàn)線程創(chuàng)建的五種寫法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,感興趣的可以跟隨小編學(xué)習(xí)一下
通過繼承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)文章
springMVC+ajax實(shí)現(xiàn)文件上傳且?guī)нM(jìn)度條實(shí)例
本篇文章主要介紹了springMVC+ajax實(shí)現(xiàn)文件上傳且?guī)нM(jìn)度條實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Java SpringBoot的相關(guān)知識(shí)點(diǎn)詳解
這篇文章主要介紹了SpringBoot的相關(guān)知識(shí)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-10-10
Mybatis批量更新數(shù)據(jù)庫錯(cuò)誤問題
這篇文章主要介紹了Mybatis批量更新數(shù)據(jù)庫錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù)
這篇文章主要介紹了Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
【MyBatis源碼全面解析】MyBatis一二級(jí)緩存介紹
下面小編就為大家?guī)硪黄綧yBatis源碼全面解析】MyBatis一二級(jí)緩存介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

