There are a number of APIs and frameworks inside the JDK, here are three parts
- Multithread Synchronization Control method
- thread pool to improve the performance of threading scheduling
- Concurrent containers for JDK
Re-entry Lock: Java.util.concurrent.locks.ReenterLock
In the code, the class Reenterlock implements the runnable, which has the static variable I, which is self-increment in the run () method.
The step for the self-increment operation is: Get-set, get the value first and then increase the value.
If there is no control here, the value of get is not the value of the latest set .
Therefore, the lock is used to control the get-set when it is added, and only one thread operation is guaranteed.
Package Com.tuniu.concurrency;import Java.util.concurrent.locks.reentrantlock;public class Lock {public static void Main (string[] args) {Reenterlock lock = new Reenterlock (); thread T1 = new Thread (lock); Thread t2 = new Thread (lock); T1.start (); T2.start ();}} Class Reenterlock implements Runnable {public static Reentrantlock lock = new Reentrantlock ();p ublic static int i = 0;PUBL IC void Run () {for (int j = 0; j<1000; j + +) {lock.lock (); try {i++;} finally {Lock.unlock ()}}}}
Java High concurrency Programming learning-JDK and Contract: Synchronization control A