Fair lock and non-fair lock __ Thread

Source: Internet
Author: User

In Reentrantlock, it is obvious that there are two kinds of synchronization, namely, fair fairsync and unfair Nonfairsync. The function of the fair lock is strictly according to the sequence of the thread start, do not allow other threads to jump in line execution, and the unfair lock is allowed to queue.

By default, Reentrantlock is synchronized through an unfair lock, including the Synchronized keyword, because it performs better. Since the thread has entered the runnable state, the start can be performed and the actual thread execution will take a long time. Also, after a lock is freed, other threads will need to retrieve the lock again. Which experienced a lock-holding thread release lock, other threads from suspend to runnable state, other threads to request locks, get locks, thread execution, this series of steps. If this time, there is a thread to request the lock directly, may avoid suspend to restore the runnable state of this consumption, so performance is more optimized.

    /**
     * Creates an instance of {@code Reentrantlock}.
     * This is equivalent to using {@code Reentrantlock (False)}.
     */Public
    Reentrantlock () {
        sync = new Nonfairsync ();
    }

Default state, the Reentrantlock () used is a fair lock. Then refer to the following code, we know that reentrantlock to get the lock operation is through the decoration mode agent to the sync.

    /**
     * Acquires the lock.
     *
     <p>acquires The lock if it is not held by another thread
     and returns * immediately, setting the lock hol D count to one.
     *
     <p>if The current thread already holds the lock then the hold * count is incremented by one and the
     meth OD returns immediately.
     *
     * <p>if The lock is held by another thread then the
     * current thread becomes disabled for thread Scheduli Ng
     * purposes and lies dormant until the lock has been, * at acquired time the
     lock which count are set to O Ne.
     */Public
    void Lock () {
        sync.lock ();
    }

The following is a reference to the Fairsync and nonfairsync implementation of the Lock method:

    /**
     * Sync object for Non-fair locks
     /
    static final class Nonfairsync extends Sync {
        /**
         * performs l Ock.  Try immediate barge, backing up to normal
         * acquire on failure.
         */
        final void Lock () {
            if (compareandsetstate (0, 1))
                Setexclusiveownerthread (Thread.CurrentThread ());
            else
                acquire (1);
        }
    }

    /**
     * Sync object for fair locks
    /static final class Fairsync extends Sync {
        final void lock () {
  acquire (1);
        }
    }

When you use a non-fair lock, will try to configure the state immediately, success will jump in line execution, failure will be the same as the mechanism of fair locking, call the Acquire () method, the exclusive way to get the lock, successfully returned immediately, otherwise the thread joined the queue, know that the successful call so far.

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.