一個(gè)Java多線程的問(wèn)題,顛覆了我多年的認(rèn)知!
碰見(jiàn)個(gè)奇怪的多線程問(wèn)題
小白們也不用怕,今天的文章你們都能看得懂😁,最近的學(xué)習(xí)中,碰到這樣的一個(gè)問(wèn)題:
Java創(chuàng)建多線程的方式有哪幾種啊?
你可能會(huì)說(shuō)啦,這還不簡(jiǎn)單,不就是:
- 繼承Thread類
- 實(shí)現(xiàn)Runnable接口
好像也是,如果你讓我回答這個(gè)問(wèn)題,我似乎也會(huì)這樣回答,頂多我會(huì)再回答一個(gè)callable的方式,但是啊,最近看到這樣的一個(gè)說(shuō)法,讓我陷入了深深的思考啊😂
Java中創(chuàng)建多線程的方法有且僅有一種,那就是new Thread的方式
嗯哼?這是怎么回事呢?這個(gè)就有點(diǎn)顛覆認(rèn)知啊,我有點(diǎn)不敢相信了,那么這到底是怎么回事呢?看到這個(gè)回答我覺(jué)得我應(yīng)該深入探討下這個(gè)問(wèn)題。
一般這問(wèn)題都是怎么問(wèn)的
關(guān)于上述說(shuō)到的這個(gè)問(wèn)題啊,并不是什么高深的問(wèn)題,而且我們大多數(shù)人都能夠回答上來(lái),只不過(guò)可能回答的不全面,我這里帶著大家去找找這個(gè)問(wèn)題在面試題中是怎么出現(xiàn)的。
首先隨便搜到了一套關(guān)于Java多線程的面試題,其中找到了關(guān)于本題的這種問(wèn)法:
你可以思考下,這個(gè)問(wèn)題讓你回答,你會(huì)怎么回答,它說(shuō)的四種,有哪四種?
這里我希望大家的著眼點(diǎn)應(yīng)該是它怎么問(wèn)的,它這里說(shuō)的是線程的實(shí)現(xiàn)方式,記住,是實(shí)現(xiàn)方式,我們繼續(xù)找找其他面試題:
在這個(gè)版本中關(guān)于這個(gè)問(wèn)題是這樣問(wèn)的,注意是創(chuàng)建線程,我們上面那一個(gè)說(shuō)的是實(shí)現(xiàn)線程,是的,就是不同的說(shuō)法,但是是一樣的嘛?
如果我們?cè)诿嬖囍斜粏?wèn)到這樣的問(wèn)題,無(wú)論是問(wèn)我們創(chuàng)建線程的方式還是實(shí)現(xiàn)線程的方式,我們的答案幾乎一定是圍繞著繼承Thread類和實(shí)現(xiàn)Runnable接口這幾個(gè)去說(shuō)的,我相信應(yīng)該不會(huì)有多少人上去就說(shuō):
創(chuàng)建線程的方式有且只有一種,那就是new Thread的方式
估計(jì)你這樣的問(wèn)答一定會(huì)被反問(wèn),為什么啊?是啊,為什么啊,其實(shí)看到這個(gè)回答,在我認(rèn)真思考了之后我覺(jué)得這個(gè)說(shuō)法是沒(méi)有啥錯(cuò)誤的。
難道我之前學(xué)的都是錯(cuò)的
我們一起來(lái)分析一下,在Java中啊,有這么個(gè)段子,就是沒(méi)有女朋友的咋辦,那就new一個(gè)啊,學(xué)習(xí)Java的都知道這是怎么回事,在Java中萬(wàn)物皆對(duì)象啊,創(chuàng)建對(duì)象一般就是new的方式了。
在Java中,Thread這個(gè)是線程類,按理說(shuō)我們創(chuàng)建一個(gè)線程對(duì)象,那就應(yīng)該是new Tread的方式啊,我們先來(lái)看我們平常都是怎么去創(chuàng)建一個(gè)線程的,一般的我們推薦實(shí)現(xiàn)接口的方式,這是源于Java的單繼承多實(shí)現(xiàn),我們來(lái)一起看下代碼:
- class MyThread implements Runnable{
- @Override
- public void run(){
- System.out.println("實(shí)現(xiàn)Runnbale的方式……");
- }
- }
當(dāng)我們寫(xiě)完上述代碼之后,我們就需要停下來(lái)思考以下了,這里我們創(chuàng)建了一個(gè)線程了嘛?我們這里貌似只是創(chuàng)建了一個(gè)實(shí)現(xiàn)了Runnbale接口的類,好像并沒(méi)有哪里有體現(xiàn)我們創(chuàng)建線程了啊,我們來(lái)做個(gè)簡(jiǎn)單的測(cè)試:
- public class Test {
- public static void main(String[] args) {
- //獲取線程數(shù)
- ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
- while(threadGroup.getParent() != null){
- threadGroup = threadGroup.getParent();
- }
- int totalThread = threadGroup.activeCount();
- System.out.println("當(dāng)前線程數(shù):"+totalThread);
- }
- }
我這里寫(xiě)了一段簡(jiǎn)單的程序,就是獲取當(dāng)前默認(rèn)線程組中有幾個(gè)線程,這段代碼你不用去管他,只需要之道它有什么用,我們運(yùn)行試一下:
這里是6,然后我們加上我們之前實(shí)現(xiàn)Runnbale那個(gè)類,一起來(lái)看下:
- public class Test {
- public static void main(String[] args) {
- //獲取線程數(shù)
- ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
- while(threadGroup.getParent() != null){
- threadGroup = threadGroup.getParent();
- }
- int totalThread = threadGroup.activeCount();
- System.out.println("當(dāng)前線程數(shù):"+totalThread);
- }
- }
- class MyThread implements Runnable{
- @Override
- public void run(){
- System.out.println("實(shí)現(xiàn)Runnbale的方式……");
- }
- }
在main方法中并沒(méi)有關(guān)于MyThread的體現(xiàn),可想,目前線程數(shù)還是6,我們一般都是怎么使用這個(gè)MyThread的呢?是不是這樣?
- public class Test {
- public static void main(String[] args) {
- Thread thread = new Thread(new MyThread());
- thread.start();
- //獲取線程數(shù)
- ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
- while(threadGroup.getParent() != null){
- threadGroup = threadGroup.getParent();
- }
- int totalThread = threadGroup.activeCount();
- System.out.println("當(dāng)前線程數(shù):"+totalThread);
- }
- }
熟悉吧,我們一般都是這樣操作的,這里想必大家也都知道,需要調(diào)用start才是真正的啟用線程,我們?cè)賮?lái)運(yùn)行下看看:
看吧,線程數(shù)增加了1,也打印出相關(guān)數(shù)據(jù)了,這才創(chuàng)建了一個(gè)線程,原因是我們寫(xiě)了這么些代碼:
- Thread thread = new Thread(new MyThread());
- thread.start();
發(fā)現(xiàn)什么沒(méi),重點(diǎn)來(lái)了,就是這里的new Thread,我們接下來(lái)看看這樣的代碼:
- public class Test {
- public static void main(String[] args) {
- new Thread();
- //獲取線程數(shù)
- ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
- while(threadGroup.getParent() != null){
- threadGroup = threadGroup.getParent();
- }
- int totalThread = threadGroup.activeCount();
- System.out.println("當(dāng)前線程數(shù):"+totalThread);
- }
- }
猜一下,現(xiàn)在的線程數(shù)是多少?
會(huì)不會(huì)有人說(shuō)是7😂,知道為什么嘛,那是因?yàn)槟銢](méi)有調(diào)用start的啊,再來(lái)看:
- public class Test {
- public static void main(String[] args) {
- new Thread().start();1
- //獲取線程數(shù)
- ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
- while(threadGroup.getParent() != null){
- threadGroup = threadGroup.getParent();
- }
- int totalThread = threadGroup.activeCount();
- System.out.println("當(dāng)前線程數(shù):"+totalThread);
- }
- }
這屬于線程的基礎(chǔ)知識(shí)了,題外話,你可知道為啥調(diào)用start而不是run嘛?
以上說(shuō)明一個(gè)什么問(wèn)題呢?真正的創(chuàng)建線程還是通過(guò)new Thread啊,然后調(diào)用start啟動(dòng)該線程,你看這個(gè):
- Thread thread = new Thread(new MyThread());
- thread.start();
也是new Thread的方式,然后構(gòu)造函數(shù)傳入一個(gè)Runnbale,我們看看Thread的構(gòu)造函數(shù)吧:
看到了吧,這里可以傳入一個(gè)Runnable,我們繼續(xù)往下思考。
創(chuàng)建線程干嘛
你想一下,我們創(chuàng)建線程干嘛,簡(jiǎn)單來(lái)說(shuō),是不是也是需要這個(gè)線程為我們干活啊,怎么干活嘞,簡(jiǎn)單來(lái)說(shuō)是不是就是這個(gè)run方法?。?/p>
- @Override
- public void run(){
- System.out.println("實(shí)現(xiàn)Runnbale的方式……");
- }
我們?cè)谶@個(gè)run方法中去執(zhí)行一些任務(wù),其實(shí)在Thread類中也有這個(gè)run方法,可以看一下:
- @Override
- public void run() {
- if (target != null) {
- target.run();
- }
- }
Thread類中的run方法沒(méi)有具體的執(zhí)行某些任務(wù),而是去執(zhí)行target中的run,這個(gè)target是啥:
- private Runnable target;
是個(gè)Runnbale,你再看看我們實(shí)現(xiàn)Runnbale的MyThread的類:
- class MyThread implements Runnable{
- @Override
- public void run(){
- System.out.println("實(shí)現(xiàn)Runnbale的方式……");
- }
- }
然后再看這個(gè):
- Thread thread = new Thread(new MyThread());
- thread.start();
我想你應(yīng)該明白了吧,這么一大圈就是為了去執(zhí)行MyThread中的run方法,因?yàn)檫@是我們新建的這個(gè)線程要干的活啊。
可能我們以前真的錯(cuò)了
我們?cè)倏纯撮L(zhǎng)說(shuō)的另一個(gè)方式,那就是繼承Thread類的形式:
- class A extends Thread {
- @Override
- public void run() {
- System.out.println("繼承Thread類的線程……");
- }
- }
這個(gè)我們知道Thread類中有這個(gè)run方法并且上面也帶大家看了,所以這里就是重寫(xiě)了run方法,而如果我們要啟動(dòng)這個(gè)線程則要這樣:
- new A().start();
這里的new A本質(zhì)還是new Thread啊,不用解釋吧,然后我們?cè)倏雌渌姆绞剑热缒涿麅?nèi)部類的方式:
- new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("匿名內(nèi)部類的方式創(chuàng)建線程");
- }
- }).start();
多么明顯,還是new Thread啊,再繼續(xù)看看實(shí)現(xiàn)callable的方式:
- class C implements Callable<Integer> {
- @Override
- public Integer call() throws Exception {
- System.out.println("實(shí)現(xiàn)callable的形式創(chuàng)建的線程");
- return 1024;
- }
- }
然后我們還需要這樣:
- FutureTask<Integer> futureTask = new FutureTask<>(new C());
- Thread thread = new Thread(futureTask);
- thread.start();
- System.out.println(futureTask.get());
這里真的創(chuàng)建線程還是new Thread的方式。
所以經(jīng)過(guò)上述的簡(jiǎn)單分析啊,我們之前的理解可能真的錯(cuò)了,我們經(jīng)常說(shuō),創(chuàng)建線程的方式有什么繼承Thread類的方式,還可以實(shí)現(xiàn)Runnable接口等等,但是現(xiàn)在看來(lái),這似乎是錯(cuò)誤的,正確的回答應(yīng)該是:
創(chuàng)建線程的方式有且僅有一種,那就是new Thread()的方式
盤(pán)點(diǎn)之前的錯(cuò)誤回答
說(shuō)到這里我覺(jué)得有必要盤(pán)點(diǎn)一下我們之前的錯(cuò)誤回答了,因?yàn)楹芏嗳思词拱凑罩暗幕卮?,要么回答的不全整,要么回答的不夠好,首先,我們看看在之前我們最完整的回答?yīng)該包含以下幾種方式:
- 繼承Thread類
- 實(shí)現(xiàn)Runnable接口
- 匿名內(nèi)部類
- 實(shí)現(xiàn)callable接口
- 使用線程池
以上五個(gè)回答是比較完整的了,一般啊,我們推薦實(shí)現(xiàn)接口的方式,這是源于java的單繼承和多實(shí)現(xiàn),另外實(shí)現(xiàn)callable和使用線程池在實(shí)際中應(yīng)用的更多。
那么有些人可能會(huì)有疑惑了,你既然你說(shuō)創(chuàng)建線程的方式有且僅有一種那就是new Thread的方式,那么上述這五種是干嘛的啊。
總結(jié)
是啊,那我們之前脫口而出的這些又是干嘛的呢?經(jīng)過(guò)我們上面的分析,我想大家應(yīng)該有看到,無(wú)論是繼承Thread類還是實(shí)現(xiàn)Runnbale,又或者其實(shí)其他方式,好像目的就是為了去實(shí)現(xiàn)那個(gè)run方法(callable的不是),準(zhǔn)確來(lái)說(shuō)就是去執(zhí)行我們真正要做的任務(wù),也就是執(zhí)行任務(wù),也就是說(shuō)啊,我們創(chuàng)建線程只有一種方式那就是new Thread的方式,但是你想啊,我們創(chuàng)建線程是讓他干活的,那干啥活嘞,我們可以通過(guò)繼承Thread類,然后重寫(xiě)run方法告訴線程該干嘛,又或者我們整一個(gè)Runnable,然后實(shí)現(xiàn)其中的run方法,然后把這個(gè)Runnable扔給Thread,告訴線程該干嘛,其他的也是同樣的道理。
那么我們是不是可以理解為:
這些都是線程執(zhí)行任務(wù)的方式,或者說(shuō)是真正實(shí)現(xiàn)線程任務(wù)的方式,但是無(wú)論怎樣,說(shuō)是創(chuàng)建線程的方式,是不是有點(diǎn)不對(duì)呢?