Learn how to synchronize the lock, monitor, mutex, synchronization event and wait handle (on) in the C # thread and provide the ultimate version of the source code.

Source: Internet
Author: User

Declaration, I am also the first contact thread, write thisArticleIt is not intended to share learning experience.

 

1. Why is the domestic version?

The original article is an implementation task. It means that two threads operate on one data simultaneously.

One thread completes operation, releases the lock, and the other thread operates, releases the lock, and loopsProgramEnd

The waiting method is

In order to achieve the opposite effect, we changed the original program

One thread waits for the other two threads to complete and then executes the operation

And use the wait (object) Method

And Relevant prompts are written for reading.CodeCan Understand the thread execution Method

2. GiveSource code

Using System;
Using System. Collections. Generic;
Using System. text;

Using System. Threading;

Namespace Threadmonitordemo
{

Class Monitorclass
{
Private   Int N =   1 ;
Private   Int M =   1 ;
Private   Int Max =   10 ;

Private   Object Creatern =   New   Object ();
Private   Object Craeterm =   New   Object ();

Public   Void Producen ()
{
Lock (Creatern)
{
For (; N < Max; n ++ )
{
Console. writeline ( " Release n = "   + N. tostring ());
// Monitor. Pulse (creatern );
Bool Isreleasen = Monitor. Wait (creatern );
Console. writeline ( " The consume caller re-acquires the creatern lock. "   + Isreleasen. tostring ());
}
}
Console. writeline ( " Producen run over " );
}

Public   Void Producem ()
{
Lock (Craeterm)
{
For (; M < Max; m ++ )
{
Console. writeline ( " Release M = "   + M. tostring ());
// Monitor. Pulse (craeterm );
Bool Isreleasem = Monitor. Wait (craeterm );
Console. writeline ( " Consume re-acquires the createrm lock. "   + Isreleasem. tostring ());
}
}
Console. writeline ( " Producem run over " );
}


Public   Void Consume ()
{
Lock (Creatern)
{
Lock (Craeterm)
{
While (M < Max && N < Max)
{
Console. writeline ( " Receive n = "   + N. tostring () +   " M = "   + M. tostring ());
Monitor. Pulse (craeterm );
// Monitor. Wait (craeterm );
Bool Isreleasem = Monitor. Wait (craeterm, 500 );
Console. writeline ( " Producem re-acquires the craeterm lock. "   + Isreleasem. tostring ());

Monitor. Pulse (creatern );
// Monitor. Wait (creatern );
Bool Isreleasen = Monitor. Wait (creatern, 500 );
Console. writeline ( " Producen re-acquires the creatern lock. "   + Isreleasen. tostring ());

}
}
}
Console. writeline ( " Consume run over " );
}
}

Class Program
{
Static   Void Main ( String [] ARGs)
{
Monitorclass m =   New Monitorclass ();

Thread tproduce =   New Thread ( New Threadstart (M. producen ));
Thread tproducem =   New Thread ( New Threadstart (M. producem ));
Thread tconsume =   New Thread ( New Threadstart (M. Consume ));

Tproduce. Start ();
Tproducem. Start ();
Tconsume. Start ();

Console. Readline ();
}
}
}

3. IntroductionA small opinion on the monitor class

The monitor class is a monitor literally. In fact, each operation of the monitor class is also a separate thread.

When several threads work at the same timeThe monitor class coordinates the work order and helps the thread to transmit messages.

AndMonitor.The wait method has a returned value. On msdn, it is true.

If the call is returned because the caller obtains the lock of the specified object again, the value is true. If the lock is not retrieved again, this method will not return.

In my understanding, if other threads obtain the Lock of this object and display the notification to other waiting threads through the monitor. Pulse (Object OBJ) method, true is returned.

Otherwise, false is returned.

Cause: the execution result is as follows when I do not display the pulse notification.

 

And wait 500 milliseconds for each execution.

To cancel monitoring. Pulse (craeterm); the execution result after the preceding annotation is

 

Each executionMonitor..::.Wait MethodReturns true.

And there is no waiting interval between the intermediate time

It seems that only the last time

SoMonitor. We can understand the return value of the wait method in this way.

If other threads obtain the Lock of this object and display the notification to other waiting threads through the monitor. Pulse (Object OBJ) method, true is returned.

Otherwise, false is returned.

 

4. execution sequence of the analysis program based on the code execution result

 

 

I drew a picture because I was not clear about it.

We should be able to see clearly the sequence of program execution.

 

This figure illustrates the sequence of program execution andMonitorThe object is controlled by a separate thread and plays a regulating role.

 

 

But there seems to be a problem just now:

 

In consume (), use the timeout wait mode isreleasem = monitor. Wait (craeterm, 1000 );

 

There was a last wait Phenomenon

 

Why ???

I haven't figured it out ....

 

If the original author is interested, he can study it together.

 

Okay, that's it.

I hope you will criticize and ,,

 

Source ThreadMonitorDemo2008-07-31.rar

 

 

 

 

Related Article

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.