Nachos Java Edition Learning (ii)

Source: Internet
Author: User

Threads. The lock class provides locks to guarantee mutual exclusion.

Executing Lock.acquire () and Lock.release () at both ends of the critical code area guarantees that only one thread accesses the critical code area , and the condition variable is built on top of the lock, by threads. Condition implementation, which is used to ensure synchronization of the tool. Each condition variable has a lock variable (the lock variable can also be performed acquire and release operations, and multiple condition variables may share the same lock variable). the thread loses the lock L and is suspended when the current thread that owns a lock L in the critical section performs a sleep operation on the condition variable that is associated with the lock L . the next thread waiting for lock L gets the lock L (this process is done by the scheduler) and enters the critical section . When the current thread in the critical section that owns the lock L performs a wake operation on the condition variable that is associated with the lock L (typically called Wake immediately after Lock.release), waits for at most one suspended thread on the condition variable (caused by the call to sleep) to be re-awakened and set to the ready state. If the wakeall operation is performed, all suspended threads waiting on the condition variable are awakened.

Threads.condition has implemented a conditional variable (using semaphore implementations), and the topic requires that the conditional variable (written in Threads.condition2) be implemented with a blocking/disable interrupt method .

1  Public classCondition2 {2     /**3 * Allocate a new condition variable.4      *5      * @paramConditionlock6 * The lock associated with this condition variable. the current7 * Thread must hold this lock whenever it uses <tt>sleep () </tt>8 * <tt>wake () </tt>, or <tt>wakeall () </tt>.9      */Ten      PublicCondition2 (Lock conditionlock) { One          This. Conditionlock =Conditionlock; A          This. Waitedthreads =NewLinkedlist<kthread>(); -     } -  the     /** - * atomically release the associated lock and go to sleep in this condition - * variable until another thread wakes it using <tt>wake () </tt>. the current - * Thread must hold the associated lock. The thread would automatically + * Reacquire the lock before <tt>sleep () </tt> returns. -      */ +      Public voidsleep () { ALib.asserttrue (Conditionlock.isheldbycurrentthread ());//determine the current process held the Block at         BooleanIntstatus =machine.interrupt (). disable (); - Waitedthreads.add (Kthread.currentthread ()); - conditionlock.release (); - kthread.sleep (); - Conditionlock.acquire (); - machine.interrupt (). Restore (intstatus); in     } -  to     /** + * Wake up at the most one thread sleeping on this condition variable. the - * Current thread must hold the associated lock. the      */ *      Public voidWake () { $Lib.asserttrue (Conditionlock.isheldbycurrentthread ());//Test If the current thread holds this lock.Panax Notoginseng         BooleanIntstatus =machine.interrupt (). disable (); -         if(!waitedthreads.isempty ()) the Waitedthreads.remove (). Ready (); + machine.interrupt (). Restore (intstatus); A     } the  +     /** - * Wake up all threads sleeping in this condition variable. the current $ * Thread must hold the associated lock. $      */ -      Public voidWakeall () { - lib.asserttrue (Conditionlock.isheldbycurrentthread ()); the          while(!Waitedthreads.isempty ()) { - Wake ();Wuyi         } the     } -  Wu     PrivateLinkedlist<kthread>waitedthreads; -     PrivateLock Conditionlock; About}

Nachos Java Edition Learning (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.