第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如下程序,為什么兩端程序中有關(guān)sleep()的語(yǔ)句都會(huì)報(bào)錯(cuò)呢?

如下程序,為什么兩端程序中有關(guān)sleep()的語(yǔ)句都會(huì)報(bào)錯(cuò)呢?

動(dòng)漫人物 2022-05-19 13:15:59
兩種創(chuàng)建線程的方法,具體如下public class MainRunT{public static void main(String[] args){MyThread mt= new MyThread();mt.start();for(int a=0;a<3;a++){System.out.println("t"+a);}}}class MyThread extends Thread{int i;public void run(){while(true){System.out.println("aaa"+i++);this.sleep(1000);if(i==5)break;}}}第二種public class MainRun{public static void main(String[] args){MyThread mt = new MyThread();Thread t = new Thread(mt);t.start();for(int a=0;a<3;a++){System.out.println("Tt"+a);}}}class MyThread implements Runnable{int i;public void run(){while(true){System.out.println("Aa"+i++);Thread.currentThread().sleep(1000);if(i==5)break;}}}問題:1。是先運(yùn)行完main中的內(nèi)容,再調(diào)用run()中的內(nèi)容,還是遇到start()就調(diào)用run()的內(nèi)容?2。為什么兩端程序中有關(guān)sleep()的語(yǔ)句都會(huì)報(bào)錯(cuò)“未處理的異常類型 InterruptedException”謝謝,在線等。
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊

Thread.currentThread().sleep(1000);?
sleep()方法是靜態(tài)(類)方法,不是實(shí)例方法,應(yīng)該是Thread.sleep(1000);sleep()方法會(huì)拋出InterruptedException異常,你不處理,當(dāng)然會(huì)報(bào)錯(cuò)
如果是先運(yùn)行完main中的內(nèi)容,再調(diào)用run()中的內(nèi)容,那就不是多線程了,而是遇到start()就調(diào)用run()的內(nèi)容,也就是說是多個(gè)線程并發(fā)(交替)運(yùn)行

查看完整回答
反對(duì) 回復(fù) 2022-05-23
?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

線程,當(dāng)使用start()開啟線程后,主main繼續(xù)向下執(zhí)行,同時(shí)線程一起運(yùn)行。(main主線程和開啟的線程并行)
sleep()方法要拋出InterruptedException,比必須處理,可以在main方法頭后面加 throws Exception把異常拋到控制臺(tái)或者try catch處理異常

查看完整回答
反對(duì) 回復(fù) 2022-05-23
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

要放在try catch語(yǔ)句結(jié)構(gòu)中

查看完整回答
反對(duì) 回復(fù) 2022-05-23
  • 3 回答
  • 0 關(guān)注
  • 348 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)