C#Monitor多线程 不阻塞线程 无法执行时 放弃

public void TestMethod1() {
            object _lock = new object();

            Action a1 = () => {
                if ( Monitor.TryEnter( _lock ) ) {
                    try {

                        for ( int i = 0; i < 10; i++ ) {
                            Debug.Print( "a1\t" + i );
                        }
                    } catch ( Exception ex ) {
                        Debug.Print( " a1 occur error : " + ex.Message );
                    } finally {
                        Monitor.Exit( _lock );
                    }
                }
            };
            Action a2 = () => {
                if ( Monitor.TryEnter( _lock ) ) {
                    try {
                        for ( int i = 0; i < 10; i++ ) {
                            Debug.Print( "a2\t" + i );
                        }
                    } catch ( Exception ex ) {
                        Debug.Print( "a2 occur error : " + ex.Message );
                    } finally {
                        Monitor.Exit( _lock );
                    }
                }
            };

            Parallel.Invoke( a1, a2 );


        }
原文地址:https://www.cnblogs.com/grj001/p/12223684.html