SuperBug

博客园 首页 联系 订阅 管理
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread newThread = new Thread(test);
            newThread.Start("para input");
            newThread = new Thread(test);
            newThread.Start("para input");
        }
        public static void test(object ob)
        {
            lock (ob)
            {
                Console.WriteLine("do something");
                Thread.Sleep(5000);
            }
        }
    }
}

输出结果为:首先输出 do something,过5秒后输出下一个do something.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread newThread = new Thread(test);
            newThread.Start("para input");
            newThread = new Thread(test);
            newThread.Start("para output");
        }
        public static void test(object ob)
        {
            lock (ob)
            {
                Console.WriteLine("do something");
                Thread.Sleep(5000);
            }
        }
    }
}

如果使用不同的参数,则不会实现互斥,两个输出语句do something同时出现。

posted on 2012-06-14 17:31  SuperBug  阅读(218)  评论(0编辑  收藏  举报