本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Java 中的读/写锁实现

发布于2024-11-10 19:04     阅读(1035)     评论(0)     点赞(10)     收藏(3)


我读了一些网页上关于锁的内容,并尝试运行一些网站描述的基本案例。我对线程使用还不熟悉,所以代码查找文件的方式如下,

1)读写锁函数(不可重入,非常基础)

public ReadWriteLock() {
    // TODO Auto-generated constructor stub
}



synchronized  void readLock(String name) throws InterruptedException {
    //tname = threadName;

    if(writers>0 || writereq>0){
        wait();
    }
    readers++;
    System.out.println(name + " locks for reading resource....");
}
synchronized  void readUnLock(String name) throws InterruptedException{
    //tname = threadName;
    readers--;
    System.out.println(name + "unlocks reading resource....");
    notifyAll();
}
synchronized  void writeLock(String name) throws InterruptedException{
    //tname = threadName;
    writereq++;
    if(writers>0 || readers>0){
        System.out.println( name + " waits for writing...");
        wait();
    }
    writereq--;
    writers++;
    System.out.println(" locks for writing resource....");
}
synchronized  void writeUnLock(String name) throws InterruptedException{
    //tname = threadName;
    writers--;
    System.out.println(name + " unlocks for writing resource....");
    notifyAll();

}

2) Runnable 接口的实现,

public class Runner implements Runnable{

private ReadWriteLock rwl;
private String name;
public Runner(ReadWriteLock rwl, String name) {
    // TODO Auto-generated constructor stub
    this.rwl=rwl;
    this.name = name;
}

void runlocks(int method){
    //String name = Thread.currentThread().getName();
    switch(method){
    case 1:
        try {
            rwl.readLock(name);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }break;
    case 2:
        try {
            rwl.readUnLock(name);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } break;
    case 3:
        try {
            rwl.writeLock(name);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } break;
    case 4:
        try {
            rwl.writeUnLock(name);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } break;

    }
}

@Override
public void run() {
    //String name = Thread.currentThread().getName();
    // TODO Auto-generated method stub
    //System.out.println("Thread started "+ name);

    //int method = 1;
    // TODO Auto-generated method stub
    //System.out.println(this.threadName + " has started!");

}

3)测试课程

public class TestClass {
 public static void main(String[] args) throws InterruptedException {
ReadWriteLock rwl = new ReadWriteLock();
Runner r1 =new Runner(rwl,"Thread1");
Thread t1 = new Thread(r1);
t1.setName("Thread1");

Runner r2 =new Runner(rwl,"Thread2");
Thread t2 = new Thread(r2);
t2.setName("Thread2");

t1.start();
t2.start();

r1.runlocks(1); //r1 locks to read
r2.runlocks(1); //r2 locks to read
r1.runlocks(2); //r1 unlocks read
r2.runlocks(2); //r1 unlocks read

r1.runlocks(3); //r1 locks to write
r2.runlocks(1); //r2 tries to lock for read but waits.. and the code gets struck here
r1.runlocks(4);  //r1 releases lock of write
}

}

我的问题是..在测试类中,线程 1 获得了写入锁,然后线程 2 尝试读取但不能读取并等待..此时应该执行下一个语句,即线程 1 解锁写入锁,线程 2 自然应该获得读取锁..但是这种情况没有发生。我是不是理解错了什么?


解决方案


您完全忽略的是,您的测试启动了两个什么都不做并立即停止运行的线程,而其余代码则由单个线程(主线程)按顺序执行。

实际上,runnable 的 run() 方法除了注释外不包含任何内容:

public void run() {
    //String name = Thread.currentThread().getName();
    // TODO Auto-generated method stub
    //System.out.println("Thread started "+ name);

    //int method = 1;
    // TODO Auto-generated method stub
    //System.out.println(this.threadName + " has started!");
}

我的建议是:阅读有关线程和 中的类的优秀教程java.util.concurrent,它们提供了易于使用的高级抽象。远离wait()和 之类的低级方法notify()



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/693324/88b3b8d59c1e2ab3e4e8/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

10 0
收藏该文
已收藏

评论内容:(最多支持255个字符)