日期:2014-05-17  浏览次数:20869 次

多线程访问list报错
请教大家一个问题

多线程访问并修改单利里的数据到底是否线程安全?是否同步?

我在单利中有一个List对象,多个线程多其进行添加或修改操作,我发现有时候会报错,提示多线程访问有冲。迷茫了...
哪位神仙指点一下
多线程? 单利 List 同步

------解决方案--------------------
public static class PermanentCache
    {
        static Dictionary<string, object> cache = new Dictionary<string, object>();
        static ReaderWriterLockSlim readWriteLock = new ReaderWriterLockSlim();

        public static object GetObject(string key)
        {
            object retValue;
            try
            {
                readWriteLock.EnterReadLock();
                cache.TryGetValue(key, out retValue);
            }
            finally
            {
                readWriteLock.ExitReadLock();
            }
            return retValue;
        }
public static void Set(string key, object obj)
        {
            readWriteLock.EnterUpgradeableReadLock();
            try
            {
                object result;
                if (cache.TryGetValue(key, out result))
                {
                    if (result != obj)
                    {
                        readWriteLock.EnterWriteLock();
                        try
                        {
                            cache[key] = obj;
        &n