日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java面试题:线程实现的两种方式及匿名内部类实现

發(fā)布時間:2024/7/5 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java面试题:线程实现的两种方式及匿名内部类实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

QUESTION:線程實(shí)現(xiàn)的兩種方式及匿名內(nèi)部類實(shí)現(xiàn)

ANSWER:

1、多線程兩種實(shí)現(xiàn)方式
(1)繼承Thread
?? ?* 定義類繼承Thread
?? ?* 重寫run方法
?? ?* 把新線程要做的事寫在run方法中
?? ?* 創(chuàng)建線程對象
?? ?* 開啟新線程, 內(nèi)部會自動執(zhí)行run方法

public class testThread2 {public static void main(String []args){Test t=new Test();//t.run(); //run()方法是普通方法,要調(diào)用start()方法才會開始線程t.start();for (int i = 0; i <3000000 ; i++) {System.out.println("b");}} } class Test extends Thread{@Overridepublic void run() {for (int i = 0; i <3000000; i++) {System.out.println("a");}} }


(2)實(shí)現(xiàn)Runnable
?? ?* 定義類實(shí)現(xiàn)Runnable接口
?? ?* 實(shí)現(xiàn)run方法
?? ?* 把新線程要做的事寫在run方法中
?? ?* 創(chuàng)建自定義的Runnable的子類對象
?? ?* 創(chuàng)建Thread對象, 傳入Runnable
?? ?* 調(diào)用start()開啟新線程, 內(nèi)部會自動調(diào)用Runnable的run()方法

public class testThread3 {public static void main(String []args){Test1 t=new Test1();Thread td=new Thread(t);td.start();for (int i = 0; i <3000000 ; i++) {System.out.println("b");}} } class Test1 implements Runnable{@Overridepublic void run() {for (int i = 0; i <300000 ; i++) {System.out.println("a");}} }

多線程兩種實(shí)現(xiàn)方式的區(qū)別
實(shí)現(xiàn)原理:
?? ?繼承Thread?? ?:?? ?由于子類重寫了Thread類的run(), 當(dāng)調(diào)用start()時, 直接找子類的run()方法
?? ?實(shí)現(xiàn)Runnable:?? ?構(gòu)造函數(shù)中傳入了Runnable的引用, 成員變量記住了它, start()調(diào)用run()方法時內(nèi)部判斷成員變量Runnable的引用是否為空, 不為空編譯時看的是Runnable的run(),運(yùn)行時執(zhí)行的是子類的run()方法
優(yōu)缺點(diǎn):
?? ?繼承Thread?? ?:
?? ??? ?好處:?? ?可以直接使用Thread類中的方法,代碼簡單
?? ??? ?弊端:?? ?如果已經(jīng)有了父類,就不能用這種方法
?? ?實(shí)現(xiàn)Runnable:
?? ??? ?好處:?? ?即使自己定義的線程類有了父類也沒關(guān)系,因?yàn)橛辛烁割愐部梢詫?shí)現(xiàn)接口,而且接口是可以多實(shí)現(xiàn)的
?? ??? ?弊端:?? ?不能直接使用Thread中的方法需要先獲取到線程對象后,才能得到Thread的方法,代碼復(fù)雜

?

多線程的安全問題及解決方案
?? ?問題:當(dāng)多線程并發(fā), 有多段代碼同時執(zhí)行時,數(shù)據(jù)會產(chǎn)生錯亂。
?? ?方案:我們希望某一段代碼執(zhí)行的過程中CPU不要切換到其他線程工作. 這時就需要同步。

public class testThread12 {public static void main(String []args){final Print p=new Print();new Thread(){@Overridepublic void run() {while (true){p.print1();}}}.start();new Thread(){@Overridepublic void run() {while (true){p.print2();}}}.start();} } class Print{test3 t=new test3();public void print1(){//synchronized (new test3())synchronized (t){ //同步代碼塊,鎖機(jī)制,鎖對象可以是任意的,不是匿名對象,因?yàn)槟涿麑ο蟮刂凡灰粯覵ystem.out.print("傳");System.out.print("智");System.out.print("播");System.out.print("客");System.out.print("\r\n");}}public void print2(){synchronized (t){System.out.print("用");System.out.print("心");System.out.print("創(chuàng)");System.out.print("造");System.out.print("\r\n");}} } class test3{}


死鎖的產(chǎn)生原理
?? ?多線程同步的時候, 如果同步代碼嵌套, 使用相同鎖, 就有可能出現(xiàn)死鎖

更多關(guān)于線程的內(nèi)容請查看本博主博客:

https://blog.csdn.net/yang13563758128/article/details/87261957

?

總結(jié)

以上是生活随笔為你收集整理的Java面试题:线程实现的两种方式及匿名内部类实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。