Java創(chuàng)建線程的兩種方式
前言
多線程是我們開發(fā)過(guò)程中經(jīng)常遇到的,也是必不可少需要掌握的。當(dāng)我們知道需要進(jìn)行多線程開發(fā)時(shí)首先需要知道的自然是如何實(shí)現(xiàn)多線程,也就是我們應(yīng)該如何創(chuàng)建線程。
在Java中創(chuàng)建線程和創(chuàng)建普通的類的對(duì)象操作是一樣的,我們可以通過(guò)兩種方式來(lái)創(chuàng)建線程:
1、繼承Thread類,并重寫run()方法。
2、實(shí)現(xiàn)Runnable接口,并實(shí)現(xiàn)run()方法。
方法一:繼承Thread類
代碼非常簡(jiǎn)單
首先重載一個(gè)構(gòu)造函數(shù),以便我們可以給線程命名。
重寫run()方法。
這里我們先讓線程輸出線程名+start。
然后每5ms輸出線程名+一個(gè)遞增數(shù)。
/**
* Created by holten.gao on 2016/10/17.
*/
public class threadThread extends Thread {
public threadThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println(this.getName()+" start!");
for(int i=0;i<10;i++){
System.out.println(this.getName()+" "+i);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
方法二:實(shí)現(xiàn)Runnable接口
代碼也非常簡(jiǎn)單
實(shí)現(xiàn)run()方法。
這里我們先讓線程輸出線程名+start。
然后每5ms輸出線程名+一個(gè)遞增數(shù)。
/**
* Created by holten.gao on 2016/10/17.
*/
public class runnableThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" start!");
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
測(cè)試結(jié)果
測(cè)試代碼
/**
* Created by holten.gao on 2016/10/17.
*/
public class Main {
public static void main(String[] args) {
Thread threadThread=new threadThread("threadThread");
threadThread.start();
Thread runnableThread=new Thread(new runnableThread(),"runnableThread");
runnableThread.start();
}
}
測(cè)試結(jié)果
threadThread start! threadThread 0 runnableThread start! runnableThread 0 threadThread 1 runnableThread 1 threadThread 2 runnableThread 2 threadThread 3 runnableThread 3 threadThread 4 runnableThread 4 threadThread 5 runnableThread 5 threadThread 6 runnableThread 6 threadThread 7 runnableThread 7 threadThread 8 runnableThread 8 threadThread 9 runnableThread 9
兩種方法比較
1.因?yàn)镴ava只支持單繼承,所以使用方法一就不能再繼承其他類了;而方法二實(shí)現(xiàn)接口則不會(huì)影響繼承其他類。
2.方法一由于是繼承Thread,所以直接new出來(lái)就可以start;而方法二需要將對(duì)象作為參數(shù)傳入Thread對(duì)象才能得到Thread對(duì)象。
3.方法一中可以直接通過(guò)this.getName獲得線程名;而方法二需要Thread.currentThread().getName()獲得
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn)
這篇文章主要介紹了身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn),本文講解了18身份證號(hào)碼的結(jié)構(gòu)、根據(jù)17位數(shù)字本體碼獲取最后一位校驗(yàn)碼程序?qū)嵗葍?nèi)容,需要的朋友可以參考下2015-06-06
JAVA設(shè)計(jì)模式零基礎(chǔ)解析之單例模式的八種方式
設(shè)計(jì)模式(Design pattern)是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過(guò)分類編目的、代碼設(shè)計(jì)經(jīng)驗(yàn)的總結(jié)。使用設(shè)計(jì)模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性2021-10-10
Java 數(shù)據(jù)庫(kù)連接池 Tomcat介紹
這篇文章主要給大家分享了 Java 數(shù)據(jù)庫(kù)連接池 Tomcat介紹,omcat 是一個(gè)小型的輕量級(jí)應(yīng)用服務(wù)器,在中小型系統(tǒng)和并發(fā)訪問(wèn)用戶不是很多的場(chǎng)合下被普遍使用,是開發(fā)和調(diào)試JSP 程序的首選。下面來(lái)看看文章內(nèi)容的詳細(xì)介紹吧2021-11-11
SpringBoot中的PUT和Delete請(qǐng)求使用
這篇文章主要介紹了SpringBoot中的PUT和Delete請(qǐng)求使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
關(guān)于SpringBoot靜態(tài)資源路徑管理問(wèn)題
這篇文章主要介紹了SpringBoot靜態(tài)資源路徑管理,主要包括默認(rèn)靜態(tài)資源路徑,增加靜態(tài)資源路徑前綴的相關(guān)操作,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
DTO 實(shí)現(xiàn) service 和 controller 之間值傳遞的操作
這篇文章主要介紹了DTO 實(shí)現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

