Waiting for subdivided threads (waitany, waitall), different from thread. Join

Source: Internet
Author: User
Keywords:
Waithandle,
Encapsulate OS-specific objects waiting for exclusive access to shared resources.
Use this static method to block a thread until one or more synchronization objects receive signals.
Waitall: wait until all specified execution threads end and return.
Waitany, Which is returned after the end of any specified execution thread.
Autoresetevent,
This class indicates a local waiting for Processing Event. After a single waiting thread is released, the event will be automatically reset upon termination.
Manualresetevent
It is not automatically reset.

Example:
Autoresetevent event1 = new autoresetevent (false );
Autoresetevent event2 = new autoresetevent (false );
Autoresetevent event3 = new autoresetevent (false );

Public void use3childthreads ()
{
Thread vthread1 = new thread (New threadstart (Method1 ));
Thread vthread2 = new thread (New threadstart (method2 ));
Thread vthread3 = new thread (New threadstart (method3 ));

Autoresetevent [] veventinprogress = new autoresetevent [3];
{
Event1,
Event2,
Event3
}

Vthread1.start ();
Vthread2.start ();
Vthread3.start ();

Waithandle. waitany (veventinprogress );

// Continue code

}

Public void Method1 ()
{
Thread. Sleep (1000 );
Event1.set ();
}

Public void method2 ()
{
Thread. Sleep (2000 );
Event2.set ();
}

Public void method3 ()
{
Thread. Sleep (3000 );
Event3.set ();
}

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.