Seven, java multithreading-based summary

First, dry goods

Three elements 1. Concurrent Programming

<1> atom of: Atomicity refers to one or more operations, and performing either all other operations will not be interrupted during execution, or to all without execution.

<2> Visibility: Visibility refers to a plurality of threads operating a shared variable, wherein the variable modify a thread, other threads can immediately see the results of changes.

<3> ordering: ordering, i.e. the order of execution of the program code in the order performed.

2. The method to achieve visibility of what

synchronized or Lock: to ensure that only one thread at the same time acquire the lock code execution, before the release of the latest value of the lock flushed to main memory, to achieve visibility.

Comparison of three methods 3. Create a thread

<1>. Uses realize Runnable, Callable way to create multi-threaded interface.

Advantages: Thread class implements Runnable interface or just Callable interface, you can also inherit from other classes. In this way, multiple threads may share the same target object, is the same for a plurality of threads to deal with a case where the same resources, which can be separated to form a clear model of the CPU, code and data, to better reflect the object-oriented thinking.

Disadvantages: slightly more complex programming, if you want to access the current thread, you must use Thread.currentThread () method.

<2> Use the Thread class inheritance way to create multiple threads

Advantages: Easy to write, if you need access to the current thread, you do not need to use Thread.currentThread () method, the direct use this to get the current thread.

Disadvantages: Thread class Thread class has been inherited, so can not inherit other parent.

Difference <3> .Runnable and a Callable

3.1) .Callable predetermined (override) is a method call (), Runnable predetermined (override) method is run ().

3.2) after .Callable task execution can return a value, but Runnable task is not the return value.

3.3) .Call method can throw an exception, run method can not.

3.4). Run Callable task can get a Future object that represents the result of an asynchronous computation. It provides a method of checking computation is complete, to wait for the completion of calculation, and calculation of the search result. By Future object can understand the implementation of the mandate, to cancel the execution of the task, but also get the results.

4. The state of the thread flow diagram

Life cycle and five basic state of the thread:

å²ä¸æ强å¤çº¿ç¨é è¯47é ¢ ¢ (å "çæ¡) ï¼å» ºè®®æ¶è

1) a new state (New) : After the object created when a thread that enters the new status, such as: Thread t = new MyThread () ;

2) the ready state (Runnable) : When the start () method (t.start (the calling thread object);), the thread that is ready state. Thread in the ready state, is illustrative of this thread is ready, waiting for the CPU scheduled for execution at any time, not to say that the implementation of the t.start () This thread will be executed immediately;

3) run state (Running) : When the CPU thread scheduling is started in a ready state, and this time was able to really execute threads that go into run mode. Note: ready state is the only means of access to the operating state, that is, the thread execution in order to enter the running state, first of all must be in a state of readiness;

4) blocking state (Blocked) : running the thread for some reason, temporarily give up the right to use the CPU, stop the execution, this time into the block until it enters the ready state, have a chance to be called again CPU to go into run mode.

Depending on the reason for blocking generated, blocked state can be divided into three types:

1. Wait for blocking: a thread of execution wait state operation () method, so that the thread enters the wait state obstruction;

2. synchronous blocking - thread synchronization lock failure in obtaining synchronized (because the lock was occupied by another thread), it will enter synchronous blocking state;

3. Other blocked - by sleep () or join the calling thread () or issued I / O request, the thread into the blocked state. When sleep () timeout, the Join () or a timeout wait for a thread to terminate, or I / O processing is completed, the thread into the ready state again.

5) death state (Dead) : thread execution is over or due to abnormal exit the run () method, the thread end of the life cycle.

5. Thread A Thread B know how to modify the variables

1) .volatile modifying variables

2) .synchronized modification method modify variables

3).wait/notify

4) .while poll (temporarily not used)

Published 122 original articles · won praise 64 · views 50000 +

Guess you like

Origin blog.csdn.net/chenmingxu438521/article/details/103814238