#大學#Java多線程學習01

來源:互聯網
上載者:User

*Java多線程基本上靠Thread類和Runnable介面來實現

*Thread類的方法

*構造方法

Thread();

Thread(Runnable runnable);//和介面一起使用,主要是協助介面實現start()方法,因為Runnable介面沒有start方法

Thread(String name);//線程名稱

Thread(Runnable runnable , String name);//4=2+3

*其它方法

start();//啟動線程

interrupt();//

join();//調用其它線程

run();//線程主體

sleep(long time);//線程休眠時間設定

wait(long time);//==sleep(long time)

wait();

notify();//啟用線程,一般和wait()搭配使用

notifyAll();

isAlive();//檢查線程狀態

stop();//強制死亡線程

*Runnable介面的方法只有一個run(),所以介面一般搭配Tread對象一起使用

 

*線程的生命週期

建立;//將需要執行多線程的語句放入run方法裡面中。

可執行; //當線程啟用start()方法後進入可執行狀態,執行run()方法。

非可執行;//可以使用Tread 類中的wait(),sleep()方法進入“非可執行狀態”。

消亡;//當run方法執行完畢後,線程自動消亡。

*線程的優先順序

Thread類中的setPriority()方法可以來設定線程的優先順序,範圍為1~10。預設是5。

*後台線程Daemon

如果所有非後台線程都結束了,則後台線程也會自動終止。

thread.setDaemon(boolean on);//當參數為true時,則標記thread線程為後台線程

thread.isDaemon();//判斷thread線程是否是後台線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.