...
int k=0;
for(int i=0;i<10;i++)
{
  Random r=new Random();
  k=r.Next(0,100);
  Thread t=new (ThreadStart(A));
  t.Start();
}void A(){
  Thread.Sleep(k);
  //...
}现在我要求 众多A之间按一定顺序执行.例如 按 i=0 to 10的 递增顺序执行,等..应该怎么做?

解决方案 »

  1.   

    如果非要使用多线程的话,,下面这段代码可能对你有帮助/*
    下面的示例演示使用 lock 关键字以及 AutoResetEvent 和 ManualResetEvent 类对主线程和两个
    辅助线程进行线程同步。有关更多信息,请参见 lock 语句(C# 参考)。 该示例创建两个辅助线程。一个线程生成元素并将它们存储在非线程安全的泛型队列中。有关更多信息,
    请参见 Queue。另一个线程使用此队列中的项。另外,主线程定期显示队列的内容,因此该队列被三个
    线程访问。lock 关键字用于同步对队列的访问,以确保队列的状态没有被破坏。 除了用 lock 关键字来阻止同时访问外,还用两个事件对象提供进一步的同步。一个事件对象用来通知辅
    助线程终止,另一个事件对象由制造者线程用来在有新项添加到队列中时通知使用者线程。这两个事件对
    象封装在一个名为 SyncEvents 的类中。这使事件可以轻松传递到表示制造者线程和使用者线程的对象。SyncEvents 类是按如下方式定义的:*/ using System; 
    using System.Threading; 
    using System.Collections; 
    using System.Collections.Generic; public class SyncEvents 

        public SyncEvents() 
        {         _newItemEvent = new AutoResetEvent(false); 
            _exitThreadEvent = new ManualResetEvent(false); 
            _eventArray = new WaitHandle[2]; 
            _eventArray[0] = _newItemEvent; 
            _eventArray[1] = _exitThreadEvent; 
        }     public EventWaitHandle ExitThreadEvent 
        { 
            get { return _exitThreadEvent; } 
        } 
        public EventWaitHandle NewItemEvent 
        { 
            get { return _newItemEvent; } 
        } 
        public WaitHandle[] EventArray 
        { 
            get { return _eventArray; } 
        }     private EventWaitHandle _newItemEvent; 
        private EventWaitHandle _exitThreadEvent; 
        private WaitHandle[] _eventArray; 

    public class Producer 

        public Producer(Queue <int> q, SyncEvents e) 
        { 
            _queue = q; 
            _syncEvents = e; 
        } 
        // Producer.ThreadRun 
        public void ThreadRun() 
        { 
            int count = 0; 
            Random r = new Random(); 
            while (!_syncEvents.ExitThreadEvent.WaitOne(0, false)) 
            { 
                lock (((ICollection)_queue).SyncRoot) 
                { 
                    while (_queue.Count < 20) 
                    { 
                        _queue.Enqueue(r.Next(0,100)); 
                        _syncEvents.NewItemEvent.Set(); 
                        count++; 
                    } 
                } 
            } 
            Console.WriteLine("Producer thread: produced {0} items", count); 
        } 
        private Queue <int> _queue; 
        private SyncEvents _syncEvents; 
    } public class Consumer 

        public Consumer(Queue <int> q, SyncEvents e) 
        { 
            _queue = q; 
            _syncEvents = e; 
        } 
        // Consumer.ThreadRun 
        public void ThreadRun() 
        { 
            int count = 0; 
            while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1) 
            { 
                lock (((ICollection)_queue).SyncRoot) 
                { 
                    int item = _queue.Dequeue(); 
                } 
                count++; 
            } 
            Console.WriteLine("Consumer Thread: consumed {0} items", count); 
        } 
        private Queue <int> _queue; 
        private SyncEvents _syncEvents; 
    } public class ThreadSyncSample 

        private static void ShowQueueContents(Queue <int> q) 
        { 
            lock (((ICollection)q).SyncRoot) 
            { 
                foreach (int item in q) 
                { 
                    Console.Write("{0} ", item); 
                } 
            } 
            Console.WriteLine(); 
        }     static void Main() 
        { 
            Queue <int> queue = new Queue <int>(); 
            SyncEvents syncEvents = new SyncEvents();         Console.WriteLine("Configuring worker threads..."); 
            Producer producer = new Producer(queue, syncEvents); 
            Consumer consumer = new Consumer(queue, syncEvents); 
            Thread producerThread = new Thread(producer.ThreadRun); 
            Thread consumerThread = new Thread(consumer.ThreadRun);         Console.WriteLine("Launching producer and consumer threads...");        
            producerThread.Start(); 
            consumerThread.Start();         for (int i=0; i <4; i++) 
            { 
                Thread.Sleep(2500); 
                ShowQueueContents(queue); 
            }         Console.WriteLine("Signaling threads to terminate..."); 
            syncEvents.ExitThreadEvent.Set();         producerThread.Join(); 
            consumerThread.Join(); 
        } }