반응형
.NET Framework 4.0부터 사용가능
다중쓰레드 안정성 보장
Dictionary보다 속도는 느림
Dictionary와 ConcurrentDictionary 다중 쓰레드 테스트
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace ConsoleApp1
{
public class Program
{
static Dictionary<string, int> _mydic = new Dictionary<string, int>();
static ConcurrentDictionary<string, int> _mydictConcu = new ConcurrentDictionary<string, int>();
static void Main()
{
Thread mythread1 = new Thread(new ThreadStart(InsertData));
Thread mythread2 = new Thread(new ThreadStart(InsertData));
mythread1.Start();
mythread2.Start();
mythread1.Join();
mythread2.Join();
Thread mythread11 = new Thread(new ThreadStart(InsertDataConcu));
Thread mythread21 = new Thread(new ThreadStart(InsertDataConcu));
mythread11.Start();
mythread21.Start();
mythread11.Join();
mythread21.Join();
Console.WriteLine(string.Format("Result in Dictionary : {0}", _mydic.Values.Count));
Console.WriteLine("********************************************");
Console.WriteLine(string.Format("Result in Concurrent Dictionary : {0}", _mydictConcu.Values.Count));
Console.ReadKey();
}
static void InsertData()
{
for (int i = 0; i < 100; i++)
{
_mydic.Add(Guid.NewGuid().ToString(), i);
}
}
static void InsertDataConcu()
{
for (int i = 0; i < 100; i++)
{
_mydictConcu.TryAdd(Guid.NewGuid().ToString(), i);
}
}
}
}
|
cs |
https://www.c-sharpcorner.com/article/concurrentdictionary-in-c-sharp/
'Windows' 카테고리의 다른 글
ARP Scan Console (0) | 2021.04.03 |
---|---|
Color Property (0) | 2021.04.03 |
C# TableLayoutPanel MouseMove event 마우스 올렸을때 셀 색상변경 (0) | 2020.11.10 |
C# 특정일 기준으로 요일(7일) 나열방법 (0) | 2020.11.04 |
C# 투명 폼 (0) | 2020.11.04 |