双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

Concurrent issue:about read write lock

reentrantlock reentrantlock 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
For the following testing code,how to make the read thread can automatically be notified when the write thread finishes writing new data to the container.
In order to improve the performance of concurrent reading data, read lock and write lock are used in the programme,beacause of this,I feel difficult to implement both of threads communicate with each other.

Anybody can provide a solution to reach the objective? Please note the pre-condition: don't use another lock to replace my read-write lock,if so,concurrent performance will be reduced.That's not what I need.
Thanks a lot in advance.

------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class ReadWriteLockTest {

private ArrayList<String> container = new ArrayList<String>();

private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

private ReadLock readLock = readWriteLock.readLock();
private WriteLock writeLock = readWriteLock.writeLock();

public void execute(){
for(int k=0;k<1;k++){
Thread writeThread = new Thread(new Runnable(){

@Override
public void run() {

while(true){
writeLock.lock();
try{
for(int j = 0;j<3;j++){
container.add("_"+j);
}

System.out.println(Thread.currentThread().getName()+" write size:"+container.size());
}
finally{
writeLock.unlock();
}

try {
System.out.println(Thread.currentThread().getName()+" sleeping...");
TimeUnit.SECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


});

writeThread.start();
}

for(int x=0;x<2;x++){
Thread readThread = new Thread(new Runnable(){

@Override
public void run() {

while(true){
readLock.lock();
try{
TimeUnit.SECONDS.sleep(1);
while(container.size()>0){
String str = container.get(0);
synchronized(container){ // remove operation need exclusive lock when read-read
container.remove(str);
}
System.out.println(Thread.currentThread().getName()+" read data:"+str+ " size:"+ container.size());
}


} catch (Exception e){
e.printStackTrace();
}finally{
readLock.unlock();
}

try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


});

readThread.start();
}

}


/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws Exception {
ReadWriteLockTest test = new ReadWriteLockTest();
test.execute();
TimeUnit.SECONDS.sleep(100000);
}

}
...全文

相关推荐

linux下实现高性能读写锁(read/write lock)

在linux下按照windows的slim read/write lock算法实现的读写锁源码。

伏神

Linux,rcu_lock

Linux,rcu_lock,路由器,网络设备

linux_Freax

one lock.zip

one lock.zip

产品经理自我修养

read-write-lock:读与写互斥,写与所有事物互斥

读写锁 写锁可防止所有其他写或读 读锁只能阻止写操作 基于! 算法 写锁定请求被放入队列并按顺序分发。 读取锁定请求也被放入队列中-但是,当给出一个读取锁定时,队列中的所有读取请求会立即发送-一旦它们全部返回,它就会进入队列中的下一个锁定请求。 用法 var createMutex = require ( 'read-write-lock' ) var mutex = createMutex ( ) mutex . writeLock ( function ( release ) { // lol I've got a write lock which means that nobody else can do anything setTimeout ( release , 200 ) } ) mutex . readLock ( function ( release ) {

孤单的宇航员

concurrent-patterns-best-practices-synchronization

I still remember the Aha! moment when I understood how UNIX shell pipeline works. I fell headlong in love with Linux and the command line and tried out many combination filters (a filter is a program reading from standard input and writes to standard output) connected via pipes. I was amazed by the creativity and power brought about by the command line. I was working with concurrent programs. Then, there was a change of project and I was thrown headlong into writing code using the multithreaded paradigm. The language was C or C++, which I loved deeply; however, to my surprise I found that it was a herculean effort to maintain a legacy code base, written in C/C++ that was multithreaded. The shared state was managed in a haphazard way and a small mistake could throw us into a debugging nightmare!

tigershin
评论 15
收藏 已收藏
分享
返回顶部
reentrantlock
Java EE 67,535 成员 225,843 内容