condition2.java

来自「Nachos 5 java version」· Java 代码 · 共 59 行

JAVA
59
字号
package nachos.threads;import nachos.machine.*;/** * An implementation of condition variables that disables interrupt()s for * synchronization. * * <p> * You must implement this. * * @see	nachos.threads.Condition */public class Condition2 {    /**     * Allocate a new condition variable.     *     * @param	conditionLock	the lock associated with this condition     *				variable. The current thread must hold this     *				lock whenever it uses <tt>sleep()</tt>,     *				<tt>wake()</tt>, or <tt>wakeAll()</tt>.     */    public Condition2(Lock conditionLock) {	this.conditionLock = conditionLock;    }    /**     * Atomically release the associated lock and go to sleep on this condition     * variable until another thread wakes it using <tt>wake()</tt>. The     * current thread must hold the associated lock. The thread will     * automatically reacquire the lock before <tt>sleep()</tt> returns.     */    public void sleep() {	Lib.assert(conditionLock.isHeldByCurrentThread());	conditionLock.release();	conditionLock.acquire();    }    /**     * Wake up at most one thread sleeping on this condition variable. The     * current thread must hold the associated lock.     */    public void wake() {	Lib.assert(conditionLock.isHeldByCurrentThread());    }    /**     * Wake up all threads sleeping on this condition variable. The current     * thread must hold the associated lock.     */    public void wakeAll() {	Lib.assert(conditionLock.isHeldByCurrentThread());    }    private Lock conditionLock;}

⌨️ 快捷键说明

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