2013年9月30日 星期一

[Java筆記] Threads

  • Thread有5個狀態
    • New
    • Runnable
    • Running
    • Waiting/Sleeping/Blocked
    • Dead
  • Modifier synchronized 不能用在constructor/variables上.
  • static synchronized method's lock is on the class-instance.
    non-static synchronized method's lock is on the object instance.
  • Thread deadlocking is the situation when thread execution grinds to a halt   
    because the code is waiting for locks to be removed from objects.      
    It occurs when both threads are waiting for each others' locks to be released.      
  • Method "long[] findDeadlockedThreads()" 用來查詢deadlock threads.   
    回傳thread IDs.   
    用於monitors或ownable synchronizers(e.g. ReentrantLock/ReentrantReadWriteLock).
    http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html#findDeadlockedThreads()
  • Memory consistency:理解 the happens-before relationship 可幫助避免 memory consistency error.    
    Happens-before relationship: 
    a guarantee that memory writes by one specific statement are visible to another specific statement.







    複習:
    • Constructors are never inherited.