Thread safe and synchronized

Security and synchronized thread

Security thread: multi-threaded access to a class that can consistently show the correct behavior, this class is thread-safe.

Simply put, the result is multi-threaded and single-threaded execution result of the implementation of consistent, not because of the different timing of the execution of multiple threads and different results

The following is a thread-safe procedure:

1.png

When this code is executed in a single thread, will arrive at the correct answer, but in a multithreaded environment, the results appeared pure luck, the results depend on the timing of the execution between threads, clearly contrary to the thread-safe Definition: multithreading access to a class that can consistently show the correct behavior.

Why is this result?

Although the count ++ looks like an operation, when the actual execution of three separate operations:

  1. Reads the value of count.

  2. The value of +1.

  3. Write count.

When this above a "read - modify - write" operation no synchronization mechanism to ensure that a three-step operation is indivisible atomic operation, which occurs three operations are alternately performed in different threads.

Just add some threads coordination mechanisms can these three operations as an atomic operation, such as synchronization lock:

2.png

There are more than a term to describe the phenomenon, called race conditions , that is, when multiple threads access the same resource, if the resource access sensitive to the order, the results depend on the order of execution threads, for the race conditions.

Synchronized java is built lock, which is a mutex, only enter the lock code is protected by a thread, so that lock protection code block atomically executed, the object may be provided lock / lock class / global security release variable functions,

3.png

Synchronized of several uses:

1. Object lock:

3.png

4.png

5.png

2. Locks:

7.png

8.png

Use the lock needs attention:

  1. When you need a lock to coordinate thread access to a variable, the variable access to all locations need to use the same lock.

2. When using a lock to make it clear whether the code block needs to be performed for a long time, such as network and IO operation, if a long time holding the lock, the thread will cause competition, the waiting thread to wait two strategies:

. A busy locks : lock release spin-wait, where appropriate block of code execution time is very short.

b.闲锁:将等待的线程挂起,锁释放后在合适的时机上下文切换,有一定内存同步代价。

jvm会对synchronized代码块进行不同程度的锁膨胀,偏向锁、轻量级锁、重量级锁,这个在后面的jvm系列博文会总结到。
当代码块执行的任务需要很长时间时尽量不要加锁。

同步代码块应尽可能短小,不需要同步的代码尽量移出,代码越短小,执行时间越短,线程竞争就越少,性能和吞吐量会更好,简单说就是快进快出

一些优化synchronized互斥锁性能的方法:

  1. 分段锁。

  2. 读写锁。

线程封闭

1. ad-hoc封闭:维护线程封闭性的职责完全由程序来承担。

例如代码中不做任何同步处理,只是把线程不安全的程序做成单线程程序,只有一个线程来执行了,自然就不会存在什么竞态条件、资源竞争,不推荐使用,建议用ThreadLocal。

在volatile变量上存在一种特殊的线程封闭。只要你能确保只有单个线程对共享的volatile变量执行写入操作,那么就可以安全地在这些共享的volatile变量上执行"读取—修改—写入"的操作。这种情况下相当于将修改操作封闭在单个线程中以防止发生竞态条件,并且volatile的可见性还确保其他的线程能看到最新的值。

  1. 栈封闭

简单说就是不用全局变量,用局部变量,方法内部声明的局部变量作用域是封闭在单个线程里的,不对其它线程共享,自然就不会出现竞态条件。

a.png
为什么栈封闭的局部变量能保证线程安全呢?后面更新的JVM系列会说明运行过程中的栈帧结构。

  1. ThreadLocal

是一种更规范的维护线程封闭性的方式,使变量和持有变量的线程关联起来,每个变量都有一份自己的变量,相互隔离防止共享。

例如实现线程与用户信息的绑定:

bb.png

不可变对象,不能修改,只读共享

Member variable immutable no additional synchronization, inherently thread-safe, if the member variable is an object, all attributes should be declared final guarantee immutability.

cc.png

Figure static jvm to ensure the safe release of all threads of the variables, the class created after the release is visible, final after release to ensure that the read-only, can not write.

When there is a thread-safe multithreaded program, select the solution of priority should be as follows:

1. Can the same objects made stateless. No state is the safest.

2. Can the thread be closed.

3. What synchronization employed.

Guess you like

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