The research of Java multithreading synchronization problem (three, lock came, everybody move aside "2")

Source: Internet
Author: User
Tags constructor count thread

"2. Fair or unfair It is a question ... "

Let's go ahead with the Reentrantlock topic.

First, Reentrantlock has a constructor with a Boolean parameter, as described in the JDK official documentation:

"The constructor method of this class accepts an optional fair parameter." When set to true, in contention for multiple threads, these locks tend to grant access to the thread with the longest waiting time. Otherwise, this lock will not guarantee any particular access order. Programs that use fair locks perform very low overall throughput (that is, slow, often extremely slow) when many threads access, compared to the default (using unfair locks), but are less varied when acquiring locks and guaranteeing the balance of lock allocations. However, it should be noted that fair locks do not guarantee the fairness of thread scheduling. As a result, a member of a number of threads using a fair lock may gain multiple chances of success, which occurs when other active threads are not processed and are not currently holding locks. Also note that the Trylock method does not use fair settings. This method can succeed, even if other threads are waiting, as long as the lock is available. ”

In simple terms: Fair locks enable threads to acquire locks in the order in which they are requested, whereas unfair locks allow for bargaining, in which case a thread can sometimes get a lock before another thread that first requests a lock.

Observe the running effect of the routines with fair and unfair locks: the sequence of threads acquiring locks has changed a bit (see table below).

unfair:

1 is running!

1 got lock1@step1!

3 is running!

2 is running!

1 Reading count:1

1 release lock1@step1!

3 got lock1@step1!

1 got lock2@step2!

Thread 1 set age to:18

Thread 1-read age is:18

3-Reading count:2

3 Release loc k1@step1!

2 got lock1@step1!

Thread 1 Second read age is:18

1 release lock2@step2!

3 got lock2@step2!

Thread 3 set age to:34

Thread 3-read age is:34

2-Reading count:3

2 releas E lock1@step1!

Thread 3 second read age is:34

3 release lock2@step2!

2 got lock2@step2!

Thread 2 set age to:72

Thread 2-read-is:72

Thread 2 second read age is:72

2 Release lock2@step2!

Successfully generated (total time: 20 seconds)
Fair:

1 is running!

1 got lock1@step1!

2 is running!

3 is running!

1 Reading count:1

1 release lock1@step1!

1 got lock2@step2!

Thread 1 set Age to:82

Thread 1-A-read age is:82

2 got lock1@step1!

2 Reading Count:2

2 release lock1@step1!

3 Got lock1@step1!

Thread 1 second Read age is:82

1 release lock2@step2!

2 got lock2@step2!

Thread 2 set Age to:65

Thread 2-A-read age is:65

3 Reading Count:3

3 Release lock1@step1!

Thread 2 second Read age is:65

2 release lock2@step2!

3 Got lock2@step2!

Thread 3 set Age to:31

Thread 3-A-read age is:31

Thread 3 second Read age is:31

3 Release lock2@step2!

Successful build (total time: 20 seconds)

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.