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); //<----DOUBT 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多线程例子讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓 Handler使用方法
- 下一篇: 浅析Java线程的三种实现