Open main menu

lensowiki β

Changes

Computer Science/162/proj1

343 bytes added, 23:58, 4 October 2008
Condition.java: formatting + copyedit
==Condition.java==
===Implementation===
;New state variablesCondition2 has a new state variable {{c|waitQueue}}, a {{c|ThreadQueue}} with {{c|transferPriority}} flag set to false.  ;Implementation details* In the {{c|sleep()}} method, we disable interrupts, the current thread is added to the {{c|waitQueue}} of threads, the corresponding lock is released, and the current thread is put to sleep. When it wakes up, the thread attempts to reacquire the condition corresponding lock and interrupts are re-enabled.
* The {{c|wake()}} method disables interrupts and retrieves the next thread from {{c|waitQueue}}. If the thread returned is not null, that thread is readied. Finally, the interrupts are re-enabled.
* The {{c|wakeAll()}} method also disables the interrupts and stored stores the next thread from {{c|waitQueue}} as a {{c|KThread}} variable , {{c|nextThread}}. While {{c|nextThread}} is not null, the method will ready that thread and reset set {{c|nextThread}} as to the next thread from {{c|waitQueue}}. This procedure continues until there are no more threads in {{c|waitQueue}}.
===Testing===
* {{c|Condition2.java}} was tested using a class called {{c|CondThread}}, which implements {{c|Runnable}}. The class is instantiated with an integer {{c|iterations}}, a lock, a {{c|Condition2}} variable to call {{c|sleep()}} on, and a {{c|Condition2}} variable to call {{c|wake()}} on. When a {{c|CondThread}} runs, it acquires the lock, then enters a for-loop of 6 iterations. On each iteration, the thread will print out a debug statement noting that the current thread is on the ith iteration. When the loop iteration number is equal to {{c|iterations}}, threads can take one of three actions.* Sleep several threads on the condition variable. Upon waking, these threads should print a unique statement and perform a {{c|wake() }} on the condition variable. Have one thread perform an initial {{c|wake() }} on the condition variable and verify that all threads are executed.* Sleep several threads on the condition variable, then have one final thread wake them all with {{c|wakeAll()}}. Verify that all threads have woken up via console print statements.
===Pseudocode===
{{c|<pre>Sleep(){ Disable Interrupts; Add current thread to wait queue; Release the lock; Put the current thread to sleep; Acquire the lock; Re-enable Interrupts;}</pre>}}
Wake(){
AssertTrue (lock held by current thread);
Disable interrupts;
If there is a thread on the wait queue, remove and wake it;
Re-enable interrupts;
}
WakeAll{{c|<pre>Wake(){ AssertTrue (lock held by current thread); Disable interrupts;While If there are threads is a thread on the wait queue, call remove and wake().it; Re-enable interrupts; }</pre>}}
{{c|<pre>WakeAll() {
Disable interrupts;
While there are threads on the wait queue, call wake().;
Re-enable interrupts;
}</pre>}}
==WaitUntil()==
1,277
edits