Java multi-threading Frequently Asked Questions

Record common concepts and principles of multi-threaded Java

reference:

How to stop a thread

  • Use volatile variables terminate running thread + Throws law / Return Method
  • Used in combination with the interrupt method interruptted / isinterrupted method terminates running thread + Throws method / Return Method
  • Use interrupt method terminates blocking a thread

Thread Safety

When multiple threads access a class, no matter what kind of scheduling runtime environment using or how these threads will alternate execution, and does not require any additional synchronization or synergistic in the calling code, the class can show the correct behavior, then this class is thread-safe.

Why wait (), notify () and notifyAll () is defined in the Object class

Wait-notify mechanism under the premise of acquiring an object lock mechanism for communication between different threads. In Java, any object can be used as a lock, since any of the lock of the object, these communication methods need to be defined in the class Object.

Why wait (), notify () and notifyAll () method must be called synchronization or sync blocks

wait / notify mechanism is dependent on Java, Synchronized synchronization mechanism, designed to ensure that the notification thread can modify the perception of shared variables made while waiting thread () returns from Wait. If used within a range of synchronization, it will throw java.lang.IllegalMonitorStateException exception.

Atomicity

Only basic types of Java variable assignment and reading an atomic operation, such as assignment i = 1, but as j = i i ++ or not such an operation is atomic because they are carried out several atomic operation, such as first read the value of i, then the value of i is assigned to j, two atomic operations are not add up to the atomic operation. So, if a variable is declared volatile, then certainly you can guarantee that every time the value of the variable values ​​obtained by reading the latest, but when they need to increment variables such non-atomic operations, would not guarantee that this variable the atomicity.

volatile keyword

  • Concept: In order to increase the processing speed, memory, and processor does not communicate directly, but first the system memory to read data internal cache (L1, L2, or other) before the operation, but the operation is not finished is written to know when RAM. If you declare a variable volatile write operation is performed, JVM will send a Lock prefix instructions to the processor, where the variable data cache line is written back to system memory. A processor's cache memory is written back to the other processor's cache can result in failure; when the processor found in the local cache invalidation, it will re-read the variable data from memory, which can get the most current value. Such volatile variables for such a mechanism makes each thread can get the latest value of the variable.
  • Features: ensuring the visibility is not guaranteed atomicity

How to ensure the security thread

  • Ensure the synchronization exclusive access to critical resources through the lock (Lock / Synchronized)
  • Use the volatile keyword, lightweight synchronization mechanism, but does not guarantee atomicity
  • And using the same thread-safe class (class atoms, concurrent containers, containers synchronization).

Guess you like

Origin www.cnblogs.com/xlsryj/p/12126450.html