readwritelock.java

来自「这个是网络上下载的一个struct框架的程序」· Java 代码 · 共 62 行

JAVA
62
字号
/** * $RCSfile: ReadWriteLock.java,v $ * $Revision: 1.4 $ * $Date: 2003/05/29 19:44:37 $ * * Copyright (C) 2002 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.struts2.framework.util;/** * Simple read write lock class that allows multiple readers but only 1 writer. Waiting writers are * treated preferentially to waiting readers. */public class ReadWriteLock {    private int readers = 0;    private boolean writingInProgress = false;    private boolean waitingWriter = false;    public ReadWriteLock() { }    public synchronized void acquireReadLock() {        while (writingInProgress || waitingWriter == true) {            try {                wait();            }            catch (InterruptedException ie) { /* ignore */ }        }        readers++;    }    public synchronized void releaseReadLock() {        readers--;        if (readers == 0) {            notifyAll();        }    }    public synchronized void acquireWriteLock() {        while (readers > 0 || writingInProgress) {            try {                waitingWriter = true;                wait();            }            catch (InterruptedException ie) { /* ignore */ }        }        writingInProgress = true;        waitingWriter = false;    }    public synchronized void releaseWriteLock() {        writingInProgress = false;        notifyAll();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?