The difference between the lock and lockinterruptibly of Java concurrency-reentrantlock

Source: Internet
Author: User

The Reentrantlock Lock Method Lock () provides an unconditional way to poll for locks, and lockinterruptibly () provides an interruptible way to obtain locks. What is the difference between these two methods? By analyzing the source code, you can know that the lock method handles the interrupt request by default, interrupts the current thread once the interrupt state is detected, and lockinterruptibly () throws the interrupt exception directly from the upper caller area.

1 lock operation

During lock acquisition, the interrupt is ignored, and after the lock is successfully acquired, the interrupt is processed according to the interrupt identity, i.e. Selfinterrupt interrupts itself. Acquire operation source code is as follows:

    /**      * Default processing interrupt mode is Selfinterrupt */public    final void acquire (int arg) {        if (!tryacquire (ARG) &&            acquirequeued (Addwaiter (node.exclusive), Arg))            Selfinterrupt ();    }
Acquirequeued, the lock is unconditionally retried in the for loop until the lock is successfully acquired and the thread interrupt state is returned. When the method returns through for normal, the lock must be successfully acquired.

    /**     * Unconditional retry until successful return and record interrupt status     *    /FINAL Boolean acquirequeued (final node node, int arg) {        Boolean failed = true;        try {            Boolean interrupted = false;            for (;;) {                final Node p = node.predecessor ();                if (p = = head && tryacquire (ARG)) {                    Sethead (node);                    P.next = null; Help GC                    failed = false;                    return interrupted;                }                if (Shouldparkafterfailedacquire (p, node) &&                    parkandcheckinterrupt ())                    interrupted = true;            }        } Finally {            if (failed)                cancelacquire (node);        }    }

2 lockinterruptibly operation

Can interrupt lock, that is, in the lock acquisition process does not handle the interrupt state, but directly throws the interrupt exception, by the upper-level caller processing interrupts. The subtle difference in the source code is that the lock gets this part of the codes, which differs from the acquirequeue in that there are two ways of returning a For loop, one for the end of the for cycle, a normal fetch to the lock, and an interrupt request immediately after the thread is awakened, which causes the method to end.

/** * acquires in exclusive interruptible mode. * @param arg the acquire argument */private void doacquireinterruptibly (int arg) throws Interruptedexceptio        n {final Node node = addwaiter (node.exclusive);        Boolean failed = true; try {for (;;)                {final Node P = node.predecessor ();                    if (p = = head && tryacquire (ARG)) {Sethead (node); P.next = null;                    Help GC failed = false;                Return                    } if (Shouldparkafterfailedacquire (p, node) && parkandcheckinterrupt ())            throw new Interruptedexception ();        }} finally {if (failed) cancelacquire (node); }    }
Conclusion: The difference between reentrantlock interrupt and non-interrupt lock mode is that after a thread attempts to acquire a lock operation fails, in the wait process, if the thread is interrupted by another thread, how it responds to the interrupt request. The lock method ignores the interrupt request, continues to acquire the lock until it succeeds, and the lockinterruptibly throws an interrupt exception, which is handled by the upper-level caller.

The difference between the lock and lockinterruptibly of Java concurrency-reentrantlock

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.