Deadlocks, live locks, performance problems

Dangerous activity

Deadlock

What is a deadlock?

A small road block to the two people together, a saying you let me go first, b said no I will not let you let me go, to each other, two people would go through.

A thread waits for the thread lock b, b-threaded task waiting for a thread lock, two threads in here trying to get a lock stop, never-ending.

1.png

Deadlock categories:

Static lock deadlock order

2.png

Look at the code can be seen directly as deadlock problem.

Dynamic Locking deadlock order

The code looks good, appear under certain extreme circumstances only run.

3.png

When fromAccount and toAccount mutual transfers simultaneously into both the first layer and the synchronized start trying to acquire a lock deadlock occurs when a second layer of synchronized.

Resource Deadlock

Locking multiple threads access a common resource.

Hunger deadlock:

Thread and other resources has been less than needed, such as transfer interface has been no response when accessing the database.

Prevent deadlock way:

A method for displaying a timing tryLock lock, after the attempt to acquire the lock timeout abstain from acquiring lock operation, try to avoid permanent.

2. Adjust the program logic, to avoid deadlock.

Livelock

Perform a same task can not be completed after the first, and then repeatedly execute a repeat of history, it has never been able to complete.

Performance and Scalability

Performance: use as few resources as possible short time to do as many operations.

Scalability: When throughput and processing power is not enough, as long as adding machines, throughput and processing power can be increased.

Multithreading overhead which will bring?

1. Context switching

2. Memory Synchronization

Synchronized and volatile memory provided visibility will cause some overhead of memory, memory barriers may be used (also known as memory barriers memory barrier) instruction cache cpu refreshed often suppressed compiler optimization operations (e.g., to prevent this part of the code weight Sort, automatically remove unnecessary lock), mainly accesses main memory and thread synchronization cpu cache.

4.png

3. obstruction

Exclusive lock is a major threat to performance and scalability.

Threat of x = lock request frequency each time the lock is occupied

The lower the threat level, the lower the degree of competition thread lock.

Methods to reduce the degree of competition in the lock:

1. Reduce lock frequency request

2. Reduction of the time lock occupation

Protected lock code should be as short as possible, not to perform a long task, no code lock protection all go out, lock granularity as small as possible (can not use class object lock lock, use the local code block do not use the method lock), Kuaijinkuaichu.

3. Use an exclusive lock with a coordination mechanism

It provides higher concurrency. (Such as segmented locks, read-write lock)

Non-blocking synchronization

Guess you like

Origin www.cnblogs.com/powerjiajun/p/11564196.html