欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-05 09:03:41
...

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();
    }
}

 

相关标签: 多线程