📄 readwritelock.java
字号:
/** * $RCSfile: ReadWriteLock.java,v $ * $Revision: 1.1 $ * $Date: 2002/07/07 16:29:08 $ * * 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.jivesoftware.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) { notify(); } } 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -