synchlist.java

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

JAVA
89
字号
package nachos.threads;import java.util.LinkedList;import nachos.machine.*;import nachos.threads.*;/** * A synchronized queue. */public class SynchList {    /**     * Allocate a new synchronized queue.     */    public SynchList() {	list = new LinkedList();	lock = new Lock();	listEmpty = new Condition(lock);    }    /**     * Add the specified object to the end of the queue. If another thread is     * waiting in <tt>removeFirst()</tt>, it is woken up.     *     * @param	o	the object to add. Must not be <tt>null</tt>.     */    public void add(Object o) {	Lib.assert(o != null);		lock.acquire();	list.add(o);	listEmpty.wake();	lock.release();    }    /**     * Remove an object from the front of the queue, blocking until the queue     * is non-empty if necessary.     *     * @return	the element removed from the front of the queue.     */    public Object removeFirst() {	Object o;	lock.acquire();	while (list.isEmpty())	    listEmpty.sleep();	o = list.removeFirst();	lock.release();	return o;    }    private static class PingTest implements Runnable {	PingTest(SynchList ping, SynchList pong) {	    this.ping = ping;	    this.pong = pong;	}		public void run() {	    for (int i=0; i<10; i++)		pong.add(ping.removeFirst());	}	private SynchList ping;	private SynchList pong;    }    /**     * Test that this module is working.     */    public static void selfTest() {	SynchList ping = new SynchList();	SynchList pong = new SynchList();	new KThread(new PingTest(ping, pong)).setName("ping").fork();	for (int i=0; i<10; i++) {	    Integer o = new Integer(i);	    ping.add(o);	    Lib.assert(pong.removeFirst() == o);	}    }    private LinkedList list;    private Lock lock;    private Condition listEmpty;}

⌨️ 快捷键说明

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