java实现读写者问题的简单代码

1.读写者问题

一个数据对象如果被多个进程所共享,且其中的一些线程只需要读取数据,而另外一些线程需要对数据进行修改。这就被称为读写者问题。

其条件要求如下

  1. 允许多个线程同时读
  2. 一次只允许一个写线程进行写操作
  3. 如果有一个写线程正在进行写操作,禁止任何线程的读操作

其实我们只需要解决两个问题,即写者和第一个读者之间的互斥,写者和写者之间的互斥。进而得到了以下代码

import java.util.concurrent.Semaphore;

/**
 * @author 12130
 */
public class ReadWriteLock {
    /* 两个互斥信号量 */
    private Semaphore readSemaphore = new Semaphore(1);
    private Semaphore writeSemaphore = new Semaphore(1);
    
    /** 
     * 读线程的个数 
     * 不知道信号量能不能保证可见性问题??需不需要volatile呢??
     */
    private int count = 0;

    /**
     * 获取读锁
     */
    public void readLock() throws InterruptedException {
        readSemaphore.acquire();
        if(count == 0){
            writeSemaphore.acquire();
        }
        count++;
        readSemaphore.release();
    }

    /**
     * 释放读锁
     */
    public void readUnLock() throws InterruptedException {
        readSemaphore.acquire();
        count--;
        if(count == 0){
            writeSemaphore.release();
        }
        readSemaphore.release();
    }

    /**
     * 获取写锁
     */
    public void writeLock() throws InterruptedException {
        writeSemaphore.acquire();
    }

    /**
     * 释放写锁
     */
    public void writeUnlock(){
        writeSemaphore.release();
    }
}

 

 测试代码,不够严谨,看看就好

import org.junit.Test;

/**
 * @author 12130
 */
public class ReadWriteLockTest {
    static int a = 10;

    @Test
    public void testRead() throws InterruptedException {
        ReadWriteLock readWriteLock = new ReadWriteLock();
        Thread t1 = new Thread(() -> {
            try {
                readWriteLock.readLock();
                System.out.println(a);
                Thread.sleep(3000);
                readWriteLock.readUnLock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                readWriteLock.readLock();
                System.out.println(a);
                Thread.sleep(2000);
                readWriteLock.readUnLock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                readWriteLock.writeLock();
                a = 100;
                System.out.println(a);
                readWriteLock.writeUnlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

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

        t3.join();
    }

    @Test
    public void testWrite() throws InterruptedException {
        ReadWriteLock readWriteLock = new ReadWriteLock();
        Thread t1 = new Thread(() -> {
            try {
                readWriteLock.readLock();
                System.out.println(a);
                readWriteLock.readUnLock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                readWriteLock.readLock();
                System.out.println(a);
                readWriteLock.readUnLock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                readWriteLock.writeLock();
                a = 100;
                System.out.println(a);
                Thread.sleep(2000);
                readWriteLock.writeUnlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });


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

        t2.join();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值