线程处理完自己的事情,就会释放,所有你可以将要处理的数据,比如前100条交给一个线程,另外100条交给另一个线程,处理完就ok了,还可以考虑线程池

解决方案 »

  1.   


    你的意思是,当一批数据被处理完之后,线程会自动释放,下一批数据来的时候,还是需要新的线程被创建来处理这批数据?
      

  2.   


    我有点明白你的意思了,你的意思是把所有的数据均衡分配给线程来执行,比如总共5000条,然后均衡分配。
    但是实际情况是,实际数据有几万到几千万不等,处理非常耗时。同时还需要记录断点(比如当处理到某一批数据时,程序异常崩溃了,或者数据库连接断开了那么我需要记录这批数据的断点)下次启动时,只需回滚这一批数据,然后再接着执行下去)这点我也很无奈....
      

  3.   


    线程池不会用啊~~~新手第一次接触多线程操作
      

  4.   

    给你一个参考    public static class X
        {
            private static System.Collections.Generic.Queue<object> queue;
            private static int queueLock;
            private static void run()
            {
                do
                {
                    while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) System.Threading.Thread.Sleep(0);
                    object value = queue.Count != 0 ? queue.Dequeue() : null;
                    queueLock = 0;
                    if (value == null) break;
                    try
                    {
                        //数据你的处理
                    }
                    catch { }
                }
                while (true);
            }
            private static void start()
            {
                queue = new System.Collections.Generic.Queue<object>();
                queue.Enqueue(new object());//添加你的数据
                for (int threadCount = 10; threadCount != 0; --threadCount) new Thread(run).Start();
            }
        }