<java><multi-thread><lock interface>

Source: Internet
Author: User

Overview
    • Describes the Java lock interface.
Motivation
    • Java has a built-in lock like synchronized, so why do I need an external lock like lock?
    • First, performance is not the reason to choose synchronized or lock, because the performance of synchronized in JDK6 is not quite the same as lock.
    • In general, select Lock is based on the following advantages of lock (built-in lock not available):
      • There can be a wait time when acquiring a lock, and there will be no longer waiting;
      • Ability to respond to interrupts when the lock is not acquired;
      • It can improve performance in the application scenario of read and write less;
      • You can return false immediately when a lock is not obtained, and return True when the lock is obtained.
Lock Interface
    • public interface lock{    void lockinterruptibly () throws interruptedexception;    Boolean trylock ();    Boolean Trylock (long time, Timeunit unit) throws interruptedexception;    void unlock ();    Condition newcondition ();}
    • which
      • When the lockinterruptibly () surface is locked, the thread that currently owns the lock can be interrupted;
      • The Trylock () is used to try to acquire the lock, which returns true if it is obtained, otherwise false is returned;
      • Trylock (long time, Timeunit unit) is similar to Trylock and will only try for a while;
      • Unlock () The thread that owns the lock releases the lock.
    • TBD ...

<java><multi-thread><lock interface>

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.