Concurrent Programming beauty

1. What is multithreading?

Multithreading allows multiple threads to work in parallel in order to complete a number of tasks in order to improve the efficiency of the system. Thread is the need to complete multiple tasks at the same time when they were implemented.

2. Understanding Multithreaded

Before we understand the multi-threaded to find out a few important concepts!

Shown above: of our project has a main memory, the main memory is stored inside our shared variable, method area, like the object heap.

3. Thread the work process

Whenever we open a thread when the thread will open up for us a working memory, the main memory copy of a copy of the shared variable is stored in the working memory, and a coordinated approach to generate stack needle area, and a reference to the stack (the pointer) .

If the thread modification operations on shared variables to work with in memory during execution, in which case we will write the variable changes back to main memory.

4. multithreading problems caused by

We simulated a scenario:

There are ten users at the same ticket, but the system only eight votes, while each user when open its own thread, the main memory is copied to the working memory 8 votes, in the process, will determine whether the number of votes meet this case, ten threads are determined to satisfy, for votes must operate.

When a user operation, votes = 8-1 = 7, the write data back to main memory.

After two user operation, a user votes two local memory is 8, the modified votes = 8-1 = 7, continue to write back to main memory,

This continues, we assume that in the case of ten users simultaneously open thread last main memory of the votes must be 7, and ten users are a successful vote, there has been oversold situation, which in reality is very dangerous scene thing!

5. Multi-threaded properties

Ordering: the program execution sequence performed in the order code.

Visibility: When multiple threads access the same variable, a thread changes the value of this variable, other threads can immediately see the modified values.

If two threads in different cpu, then thread 1 changes the value of i did not modify the thread to refresh main memory, thread 2 and i use, it is still before the affirmation of the value of i, the thread 1 of the variables did not see this is the visibility of the problem.

Atomicity: i.e. one or more operations or all operations performed and the process will not be interrupted execution of any factor, or it is not performed.

Compiled into the program during execution, the program will go through several reordering, the source code -> compiler optimization reorder -> ILP reordering -> reordering memory system -> the final execution of the instruction sequence,

That is our code, after which a series of reordering, the code is likely to write our order and inconsistent, but we will ensure that our operating system

The final sequence of instructions executed consistent with the results of our source code, we can ensure the operating system is a single-threaded orderly.

6. how to solve the problem caused by multithreading?

When do I need to use multiple threads?

Race conditions: after checking whether the execution decision satisfied next.

Method One: Lock

1. synchronized monitor lock, which ensures that each thread is isolated, and only when a thread lock synchronized method takes time to enter the execution,

When the thread is unlocking the end of this method, other threads will be suspended until after the thread unlock other threads to continue execution. It is possible to ensure that the above three characteristics: order, visibility, atomicity.

JMM define memory access specification, orderly, visibility, atomic, a total of eight rules, we can understand the JMM detailed rules for Internet information.

Synchronization mechanisms:

Monitor lock synchronized

Display lock ReentrantLock, ReadWriteLock

Atomic variables AtomicInteger, AtomicLong, AtomicBoolean

Volatile

Question: how to choose the specific problems encountered synchronous implementation?

Monitor lock after jdk1.5, performance has been greatly improved, and has been optimized in the java version of the update, the lock can be synchronized and automatically lock and unlock.

We need to manually lock display unlock, lock, easy mistakes lead to deadlock.

When considering performance, it is recommended to use the monitor lock, when considering the function, it is recommended to use the display lock, the lock display has more options to customize.

Method Two: Thread closed

What is the thread closed?

When accessing the variable data sharing, often you need to synchronize a synchronized way to avoid that is not shared data, if only in a single-threaded data access, synchronization is not required, this technique is called a thread closed.

If the thread is closed: the closure 1 Stack: the thread is a method of generating a hopping local stack variables on the use of needle threads closed.

2.ThreadLocal -> Only the current thread can be used.

Method three: Immutable objects must be thread-safe.

Best Practice: Use objects thread-safe is to achieve thread-safe. The class java.util.concurrent package.


 Reprinted to link: https: //my.oschina.net/u/3972077/blog/2222131.

Guess you like

Origin www.cnblogs.com/spring-cloud/p/11206338.html