Thread safety

Three types of thread safety:

1. Do not share variable state variables between threads.

2. Change the shared variable state variable to immutable variable. 3. Use synchronization when accessing state variables.

Thread safety implementation method:

1. Stateless objects must be thread safe. For example, the servlet does not use any global variables.

2. Atomicity: scenarios leading to atomicity: race conditions, such as counters, delay initialization; The scenario where atomicity is applied is a small number of atomic variables and irrelevant.

3. Locking mechanism. When multiple variables are involved in the invariance condition, each variable is not independent of each other, but the value of one variable will restrict the values of other variables.

The built-in lock synchronized block supports atomicity and thread reentry.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>