- Java并发教程
- 并发 - 首页
- 并发 - 概述
- 并发 - 环境设置
- 并发 - 主要操作
- 线程间通信
- 并发 - 同步
- 并发 - 死锁
- 工具类示例
- 并发 - ThreadLocal
- 并发 - ThreadLocalRandom
- 锁示例
- 并发 - 锁
- 并发 - 读写锁
- 并发 - 条件
- 原子变量示例
- 并发 - AtomicInteger
- 并发 - AtomicLong
- 并发 - AtomicBoolean
- 并发 - AtomicReference
- 并发 - AtomicIntegerArray
- 并发 - AtomicLongArray
- 并发 - AtomicReferenceArray
- 执行器示例
- 并发 - 执行器
- 并发 - ExecutorService
- ScheduledExecutorService
- 线程池示例
- 并发 - newFixedThreadPool
- 并发 - newCachedThreadPool
- newScheduledThreadPool
- newSingleThreadExecutor
- 并发 - ThreadPoolExecutor
- ScheduledThreadPoolExecutor
- 高级示例
- 并发 - Futures 和 Callables
- 并发 - Fork-Join 框架
- 并发集合
- 并发 - BlockingQueue
- 并发 - ConcurrentMap
- ConcurrentNavigableMap
- 并发 - 有用资源
- 并发 - 快速指南
- 并发 - 有用资源
- 并发 - 讨论
Java并发 - 读写锁接口
java.util.concurrent.locks.ReadWriteLock 接口允许多个线程同时读取,但一次只能有一个线程写入。
读锁 - 如果没有线程锁定 ReadWriteLock 用于写入,则多个线程可以访问读锁。
写锁 - 如果没有线程正在读取或写入,则一个线程可以访问写锁。
锁方法
以下是 Lock 类中可用的一些重要方法。
序号 | 方法和描述 |
---|---|
1 | public Lock readLock() 返回用于读取的锁。 |
2 | public Lock writeLock() 返回用于写入的锁。 |
示例
下面的 TestThread 程序演示了 ReadWriteLock 接口的这些方法。这里我们使用了 readlock() 来获取读锁,使用 writeLock() 来获取写锁。import java.util.concurrent.locks.ReentrantReadWriteLock; public class TestThread { private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); private static String message = "a"; public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new WriterA()); t1.setName("Writer A"); Thread t2 = new Thread(new WriterB()); t2.setName("Writer B"); Thread t3 = new Thread(new Reader()); t3.setName("Reader"); t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); } static class Reader implements Runnable { public void run() { if(lock.isWriteLocked()) { System.out.println("Write Lock Present."); } lock.readLock().lock(); try { Long duration = (long) (Math.random() * 10000); System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds."); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println(Thread.currentThread().getName() +": "+ message ); lock.readLock().unlock(); } } } static class WriterA implements Runnable { public void run() { lock.writeLock().lock(); try { Long duration = (long) (Math.random() * 10000); System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds."); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { message = message.concat("a"); lock.writeLock().unlock(); } } } static class WriterB implements Runnable { public void run() { lock.writeLock().lock(); try { Long duration = (long) (Math.random() * 10000); System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds."); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { message = message.concat("b"); lock.writeLock().unlock(); } } } }
这将产生以下结果。
输出
Writer A Time Taken 6 seconds. Write Lock Present. Writer B Time Taken 2 seconds. Reader Time Taken 0 seconds. Reader: aab
广告