Advanced Java (7) - Concurrent (multi-threading Basic Concepts)

First, the interrupt
interrupt interrupt method only changes the state of the target thread (interrupt status), when the thread is in wait, sleep, join and other states
are constantly checking the value of the interrupt status of the internal method, when calling interrupt throws InterruptedException exception.

interrupt method
Thread instance: After acquiring instance must be called by a thread other threads, make the call. In fact, just change the internal thread is called interrupt status;

Thread.interrupted method of
the Thread class method: must be called in the current thread of execution, the method returns the internal state of the current thread is interrupted, then clears the interrupt status (set to false)

isInterrupted method
Thread instance method: check for interrupt state of the specified thread. When a thread interrupt status, returns true; otherwise false.

Second, coordination
wait set / wait method for
each instance of the Java class has a wait set, when the object execution wait method, the current thread will be suspended, and enter the object's wait set
NOTE: To perform obj.wait current thread (), you must first acquire the object lock. When the thread into the wait set, has released the object lock.

notify method
notify method is equivalent to the wait set to pick from a thread and wakes up.
A lower figure thread wait wait set in the current instance of the object, in which case the thread B must get the same instance of the object lock before calling notify any method wake of a thread wait set.

notifyAll method
notifyAll method is equivalent to wait for all threads are set in the wake.

wait, notify, notifyAll these three methods are methods java.lang.Object class (note, not the method of the Thread class).
If these methods did not get the current thread objects directly call the object's lock, java.lang.IllegalMonitorStateException will throw an exception.

  • obj.wait () obj is the current thread into the wait set;
  • obj.notify () is to awaken a thread from the wait set obj's;
  • obj.notifyAll () are all threads in the wake of years of wait set obj.

Third, the state of the thread transfer

  • When creating a subclass or instance implementing Thread Runnable interface class, thread enters state [initial];
  • After calling start method instance, thread enters [executable] state;
  • The system will automatically schedule at some point in the [state] executable threads are scheduled thread calls the run method, enter the [implementation] state;
  • After the completion of the implementation of the thread run method, enter the [end] state;
  • [End] in the state of thread, at some point, will be JVM garbage collection;
  • [State] in the execution of the thread, if Thread.yield method is called, it will return to [perform] state, once again waiting to be scheduled;
  • [State] in the execution of the thread, if a call to the wait method, will enter the wait set and waits until they were awakened by other threads notify, notifyAll, interrupt method;
  • In execution thread [state], if called Thread.sleep method, enter [Sleep] state can not continue down. When the sleep time ends or interrupt, it will return to the [state] executable;
  • [State] in the execution of the thread, if experience blocking I / O operation will stop waiting for I / O completion, then return to the state [executable]

Fourth, immutable mode
Immutable (unchanging) participant
Immutable class is a participant field value can not be changed, there is no method to change a field value. When the instance Immutable participant is established, the state will not change completely.

Application scenarios:
advantages Immutable mode is that "do not need to use protected synchronized." And "without the use of synchronized protection," the biggest advantages is available without loss of life and property safety, and improve execution performance of the program. If the thread is shared by a majority of cases the real, and very frequently accessed, Immutable mode will be able to play a significant advantage. (Final modified one reason for the high performance variable)

Reference: https://segmentfault.com/blog/ressmix_multithread?page=3

Guess you like

Origin blog.51cto.com/4397014/2436897