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

c#多线程问题-求大侠指教!
如下代码为一个创建主键的类 但是测试多线程时锁失败了 求大侠指教!
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleUI
{
    class Program
    {
        public class KeyGen
        {
            private string Scale="";
            private Int32 Mete = 0;
            private static KeyGen m_objKeyGen;
            private KeyGen() { }
            public static KeyGen objKeyGen
            {
                get
                {
                    if (m_objKeyGen == null)
                    {
                        m_objKeyGen = new KeyGen();
                    }
                    return m_objKeyGen;
                }
            }
            public override string ToString()
            {
                lock (this)
                {
                    DateTime now = DateTime.Now;
                    string strNow = now.ToString("HHmmssffffzzz");
                    if (Scale.CompareTo(strNow)!=0)
                    {
                        Mete = 0;
                        Scale = strNow;
                    }
                    else
                    {
                        ++Mete;
                    }
                    return string.Format("{0}{1:0000}", now.ToString("yyyyMMddHHmmssfff"), Mete);
                }
            }
        }

        static void Main(string[] args)
        {
            DataTable dtKey = new DataTable();
            dtKey.Columns.Add("TKEY", typeof(string));
            dtKey.Columns.Add("THID", typeof(int));
            KeyGen objkey = KeyGen.objKeyGen;
            Thread[] thr=new Thread[100];
            for (int i = 0; i < 100; i++)
            {
                thr[i] = new Thread(delegate(object obj) {
                    for (int j = 0; j < 100; j++)
                    {
                        DataRow dr=dtKey.NewRow();
                        dr[0] = KeyGen.objKeyGen.ToString();
                        dr[1] = obj;
                        dtKey.Rows.Add(dr);
                    }
                });
            }
            for (int i = 0; i < 100; i++)
            {
                thr[i].Start(i);
            }
            new Thread(delegate() {
                while (true)
                {
                    Thread.Sleep(1000);
                    bool IsAllFull = true;
                    for (int i = 0; i < thr.Length; i++)
                    {
                        if (thr[i].ThreadState != ThreadState.Stopped) 
                        {
                            IsAllFull = false;
                            break;
                        }
                    }
                    if (IsAllFull)
                    {
                        break;
                    }
                }
                using (SqlBulkCopy sbc=new SqlBulkCopy(@"server=.\sqlexpress;uid=sa;pwd=;database=TESTDB;"))
                {
                    sbc.BatchSize = 1000;
                    sbc.DestinationTableName = "T_Key";
                    sbc.ColumnMappings.Add("TKEY", "TKEY");
                    sbc.ColumnMappings.Add("THID", "THID");
                    sbc.WriteToServer(dtKey);
                }
                Console.WriteLine("保存数据完成");
            }).Start();
        }
    }
}


/*
 CREATE DATABASE TESTDB
 GO
 
 USE TESTDB
 GO
 CREATE TABLE T_Key
 (
        TKEY varchar(30),
        THID int
 )
 */




------解决方案--------------------
别这么玩了吧 让sp哥看到了 又开骂了
明显是多线程却又要用互斥量 不知道图了个啥
------解决方案--------------------
C# code

 public static KeyGen objKeyGen