Monitor wait and pulse

Source: Internet
Author: User
ArticleDirectory
    • Monitor description
    • Use pulse and wait
    • Do not use pulse and wait
    • Conclusion
Monitor description

Enter, tryenter

Obtain the object lock. This operation also marks the beginning of the critical section. No other thread can enter the critical section unless it uses other locked objects to execute commands in the critical section.

Wait

Release the lock on the object to allow other threads to lock and access the object. When other threads access an object, the calling thread will wait. The pulse signal is used to notify the waiting thread of changes to the object state.

Pulse (signal), pulseall

Sends signals to one or more waiting threads. This signal indicates that the status of the waiting thread lock object has changed, and the lock owner is ready to release the lock. Wait for the thread to be placed in the ready queue of the object so that it can finally receive the object lock. Once the thread has a lock, it can check the new State of the object to see if it has reached the desired state.

Exit

Release the lock on the object. This operation also marks the end of the critical section protected by the locked object.

Use pulse and wait

Public void docriticalsection ()

{

Monitor. Enter (this. lockme );

Console. writeline (string. Format ("docriticalsection lock held by threadid: {0}", thread. currentthread. managedthreadid ));

For (INT I = 0; I <5; I ++)

{

Monitor. Pulse (this. lockme );

Console. writeline (string. Format ("thread ID: {0} pulse", thread. currentthread. managedthreadid ));

Console. writeline (string. Format ("monitorwaitandpulse1 result: {0}", result ++ ));

Thread. Sleep (1000 );

Monitor. Wait (this. lockme );

Console. writeline (string. Format ("thread ID: {0} Wait", thread. currentthread. managedthreadid ));

}

Console. writeline (string. Format ("docriticalsection lock realestate by threadid: {0}", thread. currentthread. managedthreadid ));

Monitor. Exit (this. lockme );

}

Do not use pulse and wait

Public void docriticalsection ()

{

Monitor. Enter (this. lockme );

Console. writeline (string. Format ("docriticalsection lock held by threadid: {0}", thread. currentthread. managedthreadid ));

For (INT I = 0; I <5; I ++)

{

// Monitor. Pulse (this. lockme );

Console. writeline (string. Format ("thread ID: {0} pulse", thread. currentthread. managedthreadid ));

Console. writeline (string. Format ("monitorwaitandpulse1 result: {0}", result ++ ));

Thread. Sleep (1000 );

// Monitor. Wait (this. lockme );

Console. writeline (string. Format ("thread ID: {0} Wait", thread. currentthread. managedthreadid ));

}

Console. writeline (string. Format ("docriticalsection lock realestate by threadid: {0}", thread. currentthread. managedthreadid ));

Monitor. Exit (this. lockme );

}

Conclusion

use monitor. wait and monitor. the pulse can be executed on threads alternately. If you only use enter and exit, when Code enters monitor. after entering, the current thread does not monitor. exit. Other threads can only wait. monitor. after pulse, the lock can be obtained and executed in the next line. Monitor. wait and wait for other threads to release the lock.

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.