java 分离锁_PLock 单高效的跨进程锁,支持读写锁分离

512%20horizontal.png

PLock

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

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d417061636865253230322e302d626c75652e73766768747470733a2f2f6a69747061636b2e696f2f762f7071706f2f504c6f636b2e737667

If you like this, welcome to star/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

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

} finally {

pLock.unlock();

}

// in non-blocking mode

if(pLock.tryLock()) {

try {

// to do something...

} catch (Exception e) {

// errors

} finally {

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.

PLock-first-process

PLock-second-process

screenshot_plock_first.jpg

screenshot_plock_second.jpg

About Me:

GitHub: pqpo

Twitter: Pqponet

WeChat: pqpo_me

qrcode_for_gh.jpg

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值