PLock:简单易用并且高效的 Android 跨进程锁

PLock:简单易用并且高效的 Android 跨进程锁

Android小彩虹2021-09-11 8:12:202460A+A-

PLock is a simple and efficient cross-process lock, also supports read-write lock.

License

If you like this, welcome to start/fork it or follow me.

PLock is an Android library, it makes Android to support cross-process lock, not only that, it can also be transfer to support Java project.
It is easy to use and runs efficiently.
PLock can acquire locks in either blocking(lock) or non-blocking(tryLock) mode. In blocking mode, it will block the current thread in the process if the lock you acquire is already hold by another process, that only returns false in non-blocking mode.
PLock also supports read-write lock cross process,If one process holds a read lock, the other process can also acquire a read lock successfully,and cannot acquire a write lock. If one process holds a write lock, the other process cannot acquire any read or write lock.
You can also use PLock as a file lock, In fact, it is based on file locks that using fcntl

acquire read lock acquire write lock
no lock allow allow
one or more read locks allow disallow
one write lock disallow disallow

Getting started

In your build.gradle:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.pqpo:PLock:{last-version}'
}

Usage

  • Step 1. get a default PLock object, and you can also create one by yourself.
  • Step 2. acquire locks.
  • Step 3. unlock.
  • Step 4. release if necessary.

For example:

PLock pLock = PLock.getDefault();
// OR you can also use it as a file lock:
// PLock pLock = new PLock(file);

pLock.lock(); 
try {
    // to do something...
} catch (Exception e) {
    // errors
} final {
    pLock.unlock(); 
}

// in non-blocking mode
if(pLock.tryLock()) {
   try {
       // to do something...
   } catch (Exception e) {
       // errors
   } final {
       pLock.unlock(); 
   }
} 

// non-block read lock
if(pLock.tryReadLock()) {
    // to do something...
} 
// non-block write lock
if(pLock.tryWriteLock()) {
    // to do something...
} 

// etc.


// pLock.release(); 

Play with samples, have fun!

You can clone this repository, then run 'debug' build variant and 'second' build variant, after that you can see two applications running on your phone which named PLock-FirstProcess and PLock-SecondProcess.

Or download PLock-FirstProcess and PLock-SecondProcess.

PLock-first-process PLock-second-process

About Me:

License

Copyright 2018 pqpo Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示 | 支付宝红包
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1
本网站由 提供CDN/云存储服务

联系我们