日期:2014-05-18  浏览次数:21008 次

线程问题,如何让一个对象只能打开一个线程
两个对象t1,t2,执行时间不同,当是t1对象的时候,在当前线程执行Execute方法,当t2对象的时候,因为t2执行慢,需要新开一个线程执行Execute方法,但是按照目前的程序,可能会有多个线程同时执行t2的excute方法,想寻找一种方法能够控制住,如果t2的excute在执行,就不要创建这个线程了,等执行完毕,在主线程中在开启新线程去执行,既每个线程对应到对象上。

C# code
using System;
using System.Threading;

namespace 一个对象一个线程
{
    class Program
    {
        static void Main(string[] args)
        {
            var t1 = new MyClass {WaitTime = 1000};
            var t2 = new MyClass { WaitTime = 6000 };
            int i = 0;

            while (true)
            {
                var curr=new MyClass();
                if (i++ % 2 == 0)
                {
                    Execute(t1);
                }
                else
                {
                    curr = t2;
                    var thread = new Thread(new ParameterizedThreadStart(Execute));
                    Console.WriteLine("开始新线程id:" + thread.ManagedThreadId);
                    thread.Start(t2);
                }
                Thread.Sleep(1000);
            }
        }

        static void Execute(object obj)
        {
            var me = (MyClass) obj;
            if (me.WaitTime == 1000)
            {
                Console.WriteLine("开始执行");
                Thread.Sleep(me.WaitTime);
                Console.WriteLine("执行完毕");
            }
            else
            {
                {
                    Console.WriteLine("-------------------开始执行");
                    Thread.Sleep(me.WaitTime);
                    Console.WriteLine("-------------------执行完毕");
                }
            }
        }
    }

    class MyClass
    {
        public int WaitTime { get; set; }
    }
}



------解决方案--------------------
加个标识不就完事了
------解决方案--------------------
C# code

      static void Main(string[] args)
        {
            var t1 = new MyClass {WaitTime = 1000};
            var t2 = new MyClass { WaitTime = 6000 };
            int i = 0;

            while (true)
            {
                var curr=new MyClass();
                if (i++ % 2 == 0)
                {
                    Execute(t1);
                }
                else
                {
                    curr = t2;
                    if (!curr.IsRun)
                    {
                        var thread = new Thread(new ParameterizedThreadStart(Execute));
                        Console.WriteLine("开始新线程id:" + thread.ManagedThreadId);
                        thread.Start(t2);
                    }
                }
                Thread.Sleep(1000);
            }
        }

        static void Execute(object obj)
        {           
            var me = (MyClass) obj;
            me.IsRun = true;
            if (me.WaitTime == 1000)
            {
                Console.WriteLine("开始执行");
                Thread.Sleep(me.WaitTime);
                Console.WriteLine("执行完毕");
            }
            else
            {
                {
                    Console.WriteLine("-------------------开始执行");
                    Thread.Sleep(me.WaitTime);
                    Console.WriteLine("-------------------执行完毕");
                }
            }
            me.IsRun = false;
        }
    }

    class MyClass
    {
        public int WaitTime { get; set; }
        private bool _isrun = false;
        public bool IsRun
        {
            get { return this._isrun; }
            set { this._isrun = value; }

        }
    }