java多线程详细讲解_Java多线程例子讲解
一:知識點聲明:
1.區別進程和線程:進程是靜態概念,它的執行依賴線程進行。
2.進程的狀態:就緒(等待cpu執行),運行,中止,阻塞(等待所需資源,進入阻塞態)
3.Java程序的main函數即是一個線程,被稱做主線程。此時如果新建線程,則和主線程一起并行運行。
4.Java中的構造方法、main函數誰先執行?
main函數先執行,因為main是靜態方法,程序一開始就執行;而構造方法只有在類實例化時才去調用。
二:實例程序
public class GetCurrentThread implements Runnable {
Thread th;
public GetCurrentThread(String threadName) {
th = new Thread(this,threadName); //
System.out.println("get threadname "+th.getName());
th.start();
}
public void run() {
System.out.println(th.getName()+" is starting.....");
System.out.println("Current thread name : " + Thread.currentThread().getName());
}
public static void main(String args[]) {
System.out.println("Current thread name : " + Thread.currentThread().getName());
new GetCurrentThread("1st Thread");
//new GetCurrentThread("2nd Thread");
}
}
三:執行結果
四:程序分析過程(直接用筆記
)
總結
以上是生活随笔為你收集整理的java多线程详细讲解_Java多线程例子讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果 iPhone 15 Pro 前玻璃
- 下一篇: 约合人民币5000元!低音炮Sonos