Java多线程例子讲解
生活随笔
收集整理的這篇文章主要介紹了
Java多线程例子讲解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一:知識(shí)點(diǎn)聲明:
1.區(qū)別進(jìn)程和線程:進(jìn)程是靜態(tài)概念,它的執(zhí)行依賴線程進(jìn)行。
2.進(jìn)程的狀態(tài):就緒(等待cpu執(zhí)行),運(yùn)行,中止,阻塞(等待所需資源,進(jìn)入阻塞態(tài))
3.Java程序的main函數(shù)即是一個(gè)線程,被稱做主線程。此時(shí)如果新建線程,則和主線程一起并行運(yùn)行。
4.Java中的構(gòu)造方法、main函數(shù)誰先執(zhí)行?
main函數(shù)先執(zhí)行,因?yàn)閙ain是靜態(tài)方法,程序一開始就執(zhí)行;而構(gòu)造方法只有在類實(shí)例化時(shí)才去調(diào)用。
二:實(shí)例程序
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"); } }
四:程序分析過程(直接用筆記)
總結(jié)
以上是生活随笔為你收集整理的Java多线程例子讲解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓 Handler使用方法
- 下一篇: 浅析Java线程的三种实现