最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

關(guān)于接口的使用

為什么代碼中的第二個(gè)類是用接口來寫的呢

正在回答

1 回答

繼承Thread和實(shí)現(xiàn)Runnable其區(qū)別主要在于共享數(shù)據(jù),Runnable接口是可以共享數(shù)據(jù)的,多個(gè)Thread可以同時(shí)加載一個(gè)Runnable,當(dāng)各自Thread獲得CPU時(shí)間片的時(shí)候開始運(yùn)行Runnable,Runnable里面的資源被共享。

而例子中

class?Actress?implements?Runnable{

????static?int?count?=?0;
????@Override
????public?void?run()?{
????????System.out.println(Thread.currentThread().getName()?+?"是一個(gè)演員!");
????????boolean?courrent?=?true;
????????while(courrent)?{
????????????System.out.println(Thread.currentThread().getName()?+?"登臺(tái)演出第"?+?(++count)?+?"場(chǎng)次!");
????????????if(count>=100){
????????????????courrent?=?false;
????????????}
????????????if(count%10?==?0){
????????????????try?{
????????????????????Thread.sleep(100);
????????????????}?catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}
????????System.out.println(Thread.currentThread().getName()?+?"演出結(jié)束了");
????}
}


public?class?Actor?extends?Thread?{

????static?int?count?=?0;
????public?void?run()?{
????????System.out.println(getName()?+?"是一個(gè)演員!");
????????boolean?courrent?=?true;
????????while(courrent)?{
????????????System.out.println(getName()?+?"登臺(tái)演出第"?+?(++count)?+?"場(chǎng)次!");
????????????if(count>=100){
????????????????courrent?=?false;
????????????}
????????????if(count%10?==?0){
????????????????try?{
????????????????????Thread.sleep(1000);
????????????????}?catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}
????????System.out.println(getName()?+?"演出結(jié)束了");
????}

????public?static?void?main(String[]?args)?{
????????Thread?actorThread?=?new?Actor();
????????actorThread.setName("Mr.Thread");
????????actorThread.start();
????????Thread?actress?=?new?Thread(new?Actress(),"Ms.Runnable");
????????actress.start();
????}
}

主要是說明不管是繼承Thread還是實(shí)現(xiàn)Runnable接口我們都可以創(chuàng)建線程。在實(shí)際開發(fā)中大多數(shù)情況下是實(shí)現(xiàn)Runnable接口的,因?yàn)樗梢怨蚕頂?shù)據(jù)。

1 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

關(guān)于接口的使用

我要回答 關(guān)注問題
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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