zoukankan      html  css  js  c++  java
  • 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 );
    
    
            }
  • 相关阅读:
    LeetCode-Minimum Path Sum
    LeetCode-Validate Binary Search Tree
    LeetCode-Recover Binary Search Tree
    LeetCode-Valid Parentheses
    LeetCode-Generate Parentheses
    LeetCode-4Sum
    BZOJ 2295: [POJ Challenge]我爱你啊
    BZOJ 2725: [Violet 6]故乡的梦
    BZOJ 3672: [Noi2014]购票
    BZOJ 4327:[JSOI2012]玄武密码
  • 原文地址:https://www.cnblogs.com/grj001/p/12223683.html
Copyright © 2011-2022 走看看