Java High concurrency Programming learning-JDK and Contract: Synchronization control A

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.