搜尋此網誌

2011年6月27日 星期一

Java Thread筆記

以下內容節錄自http://download.oracle.com/javase/tutorial/essential/concurrency/
  1. The Runnable object is passed to the Thread constructor for execution.
  2. Thread.sleep causes the current thread to suspend execution for a specified period.
  3. Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.
  4. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.
  5. Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:
  6. Allowing a thread to acquire the same lock more than once enables reentrant synchronization. This describes a situation where synchronized code, directly or indirectly, invokes a method that also contains synchronized code, and both sets of code use the same lock. Without reentrant synchronization, synchronized code would have to take many additional precautions to avoid having a thread cause itself to block.
  7. Deadlock describes a situation where two or more threads are blocked forever, waiting for each other
  8. Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress.
  9. As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked — they are simply too busy responding to each other to resume work.
  10. guarded block. Such a block begins by polling a condition that must be true before the block can proceed.