java 傳統多執行緒

2021-09-02 16:07:58 字數 1194 閱讀 2558

/*** 傳統多執行緒

*/public class traditionalthread catch (interruptedexception e)

system.out.println("1:"+thread.currentthread().getname());

system.out.println("2:"+this.getname());}}

};thread.start();

thread thread2=new thread(new runnable() catch (interruptedexception e)

system.out.println("1:"+thread.currentthread().getname());

"2:"+this.getname());}}

});thread2.start();

//建立了乙個 thread 的子類重寫了run方法,並且 給建構函式傳遞了 runnable介面的例項 ,結果呢只會執行 thread子類的run方法不會執行runnable介面

new thread(new runnable() catch (interruptedexception e)

system.out.println("runnable:"+thread.currentthread().getname());

"2:"+this.getname());}}

}) catch (interruptedexception e)

system.out.println("thread:"+thread.currentthread().getname());

"2:"+this.getname());}}

}.start();

//建立了乙個 thread 的子類沒有重寫了run方法,並且 給建構函式傳遞了 runnable介面的例項 ,結果呢會執行 trunnable介面中run方法中**

new thread(new runnable() catch (interruptedexception e)

system.out.println("runnableas:"+thread.currentthread().getname());

"2:"+this.getname());}}

}){}.start();}}

傳統多執行緒技術

執行緒有2種方式 1 重寫thread的run方法 例子 new thread start 2 thread中構造方法有帶引數的傳入runable 推薦 例子 new thread new runable public void run start 解釋 thread類中的run方法 為 priva...

java併發程式設計(1) 傳統多執行緒技術

1.傳統兩種方式實現執行緒 package com.qunar.thread public class traditionalthread catch interruptedexception e system.out.println thread.currentthread getname sys...

java多執行緒

在網上看到很有意思的問題,摘下來好好看下 在面試的時候被問了乙個多執行緒的問題 回來仔細思考了一下,多執行緒是否真的能提高了效率?我對多執行緒的理解就是 比如挖乙個隧道,有2種開工方法 1 只在山的一頭挖,直至挖到山的另一頭,從而打通隧道,這可以看成是單執行緒 2 在山的兩頭挖,同時開工,最後在山的...