Java Thread 注意事項

來源:互聯網
上載者:User
  1. 該同步要同步
  2. 同步塊儘可能的小
  3. 迴圈內使用wait()
    1. synchronized
       (obj) {  
    2.     while
       (condition) {  
    3.         obj.wait();  
    4.     }  
    5. }  
    synchronized (obj) {    while (condition) {        obj.wait();    }}
  4. notifyAll()優先於notify()
  5. yield()不可靠
  6. 在持有鎖的時候, 盡量不要調用其它對象的方法, 因為這很可能是死結的源頭.
  7. 線程優先順序取決於底層實現, 應盡量避免使用.
  8. 守護線程與非守護線程的區別:當進程中所有非守護線程已(結束或)退出時,即使仍有守護線程在運行,進程仍將結束.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.