The tryenter () method of the monitor class is similar to the enter () method in trying to obtain an explicit lock on an object. However, it does not block execution like the enter () method. If the thread successfully enters the key area, the tryenter () method returns true.
Two of the three overload methods of the tryenter () method use a timeout value as the parameter, indicating that the lock is waiting for the specified time. Let's look at an example of how to use tryenter (), monitortryenter. CS:
/*************************************
/* Copyright (c) 2012 Daniel Dong
*
* Author: odaniel Dong
* Blog: O www.cnblogs.com/danielwise
* Email: O guofoo@163.com
*
*/
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Threading;
Namespace monitortryenter
{
Public class tryenter
{
Public tryenter ()
{
}
Public void criticalsection ()
{
Bool B = monitor. tryenter (this, 1000 );
Console. writeline ("Thread"
+ Thread. currentthread. gethashcode ()
+ "Tryenter value" + B );
If (B)
{
For (INT I = 1; I <= 3; I ++)
{
Thread. Sleep (1000 );
Console. writeline (I + ""
+ Thread. currentthread. gethashcode () + "");
}
}
If (B)
{
Monitor. Exit (this );
}
}
Public static void main ()
{
Tryenter A = new tryenter ();
Thread T1 = new thread (New threadstart (A. criticalsection ));
Thread t2 = new thread (New threadstart (A. criticalsection ));
T1.start ();
T2.start ();
Console. Readline ();
}
}
}
A possible output result is as follows:
The tryenter () method is useful when resource competition occurs and you are not as useful as putting the thread sleep for an unexpected period of time. This is a good example of dialing to the ISP. Assume there are two programs a and B, both of which want to use the same modem to dial the ISP. Once the connection is established, there will only be one network connection, and we do not know how long the existing application will connect. Suppose that program a calls the ISP first, and program B also calls the ISP. No doubt program B will wait because we don't know how long program a will connect. In this case, program B may use tryenter () to determine whether the modem has been locked by another application (in this example, program a), rather than using enter () method.
Lock keyword
The lock keyword can be used as an alternative to the monitor class. The following two code blocks are equivalent:
Monitor. Enter (this );
//...
Monitor. Exit (this );
Lock (this)
{
//...
}
In the following example, locking. CS uses the lock keyword instead of the monitor method:
/*************************************
/* Copyright (c) 2012 Daniel Dong
*
* Author: Daniel Dong
* Blog: www.cnblogs.com/danielwise
* Email: guofoo@163.com
*
*/
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Threading;
Namespace lock
{
Class lockword
{
Private int result = 0;
Public void criticalsection ()
{
Lock (this)
{
// Enter the critical section
Console. writeline ("entered thread"
+ Thread. currentthread. gethashcode ());
For (INT I = 1; I <= 5; I ++)
{
Console. writeline ("result =" + Result ++
+ "Threadid"
+ Thread. currentthread. gethashcode ());
Thread. Sleep (1000 );
}
Console. writeline ("exiting thread"
+ Thread. currentthread. gethashcode ());
}
}
Public static void main (string [] ARGs)
{
Lockword E = new lockword ();
Thread T1 = new thread (New threadstart (E. criticalsection ));
T1.start ();
Thread t2 = new thread (New threadstart (E. criticalsection ));
T2.start ();
// Wait till the user enters something
Console. Readline ();
}
}
}
The output of locking. CS is the same as that of monitorenterexit (a parameter is required:
Author: danielwise
Source: http://www.cnblogs.com/danielWise/