One. Overview
Multithreading can help us achieve concurrency, but concurrency poses some problems.
[1] Thread safety issues
[2] Active issues
[3] Performance issues
Two. The problem of activity
Common activity issues are:
[1] Deadlock:
The classic deadlock problem has a philosopher's problem, and when a deadlock occurs, the program cannot continue running.
The main cause of the deadlock problem is:
Only partial resources are obtained, but the current resource is not discarded.
[2] Hunger
Because of a thread priority issue. Some threads may not run for a long time.
Classic problem: High-priority threads consume time slices for a long time, and other low-priority threads are starved.
[3] Live Lock
The classic question:
Two threads mutually humble resources, but have not abandoned the resources after the acquisition of other resources.
such as giving way to the problem.
A live lock is much more dangerous than a deadlock, and it is not easy to detect.
Deadlock Detection:
We can use tools to detect deadlocks, but it's hard to detect hunger and live locks.
Three. Performance issues
Multi-threaded concurrency is the root of the time slice in the switch, but the time slice of the switch takes time.
When switching time slices takes more time, the resources that were originally given to the program are switched by time slices.
Use, then there is a problem with performance.
The core of the performance problem is:
Determine if concurrency, how much concurrency, and the amount of concurrent time are required.
Four. Security issues
When multithreaded execution, there will be contention for resources, if the control is not appropriate, then there will be incorrect results.
This is a thread-safety issue.
Solutions to security issues:
Use thread synchronization to resolve the issue.
007 Thread Risk