Java Core Learning (19) Javanio Framework---file lock

Source: Internet
Author: User

  

In the FileChannel mentioned in the previous section, the Lock ()/trylock () method is available to obtain a file lock Filelock object, which locks the file, where the file lock is a process-level lock, not a thread lock.

The lock () method is a blocking test, and when the method is called and the file lock is not available, the program blocks.

The Trylock () method is a non-blocking locking method that returns the file lock if a file lock is obtained, otherwise NULL is returned.

File locks are divided into shared and exclusive locks, and when the third argument of the Lock/trylock method indicates that the lock is a shared lock, a shared lock allows multiple cities to read the file, but prevents other processes from obtaining an exclusive lock on the file, which by default is False, which is an exclusive lock.

Note: 1, the file lock is held by the Java Virtual machine, if two Java programs run with the same Java virtual machine, you can not lock the same file

2. File lock on some platforms is not mandatory, so it does not work.

     

Java Core Learning (19) Javanio Framework---file lock

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.