Ver3.0 ๐ C# ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ๊ณผ async/await, Task๋ฅผ ํตํ ๊ณ ์ฑ๋ฅ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ ์๋ฒฝ ๊ฐ์ด๋

๐ C# ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ๊ณผ async/await, Task๋ฅผ ํตํ ๊ณ ์ฑ๋ฅ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ ์๋ฒฝ ๊ฐ์ด๋
์น๊ตฌ์ฒ๋ผ ์ฝ๊ฒ ๋ฐฐ์ฐ๋ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๋ชจ๋ ๊ฒ
์๋
! ๐ ์ค๋์ C# ๊ฐ๋ฐ์๋ผ๋ฉด ๋ฐ๋์ ์์์ผ ํ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๋ํด ์ด์ผ๊ธฐํด๋ณผ๊ฒ. ํน์ ํ๋ก๊ทธ๋จ์ด ๋ฉ์ถ ๊ฒ์ฒ๋ผ ๋๊ปด์ง๊ฑฐ๋, ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ๋๋ ๋๋ฐ ํ์ฐธ์ ๊ธฐ๋ค๋ ค์ผ ํ๋ ๊ฒฝํ ์์ง? ๐ฐ ๋ฐ๋ก ๊ทธ๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ด์ผ!
์์ฆ ์ ํ๋ฆฌ์ผ์ด์
์ ๋คํธ์ํฌ ํต์ , ํ์ผ ์
์ถ๋ ฅ, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฟผ๋ฆฌ ๋ฑ ์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ์์
๋ค์ ์์ฒญ ๋ง์ด ํด. ์ด๋ฐ ์์
๋ค์ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ์ง ๋ชปํ๋ฉด ์ฌ์ฉ์ ๊ฒฝํ์ด ์ต์
์ด ๋์ง. ๐ฑ ๊ทธ๋์ ๋ฑ์ฅํ ๊ฒ ๋ฐ๋ก async/await์ Task์ผ!
๐ฏ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ด ๋ญ๊ธธ๋?
๋จผ์ ๊ธฐ๋ณธ ๊ฐ๋
๋ถํฐ ํ์คํ ์ก๊ณ ๊ฐ์! ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ์ดํดํ๋ ค๋ฉด ๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ๊ณผ ๋น๊ตํด๋ณด๋ ๊ฒ ์ ์ผ ์ข์.
๋๊ธฐ(Synchronous) ํ๋ก๊ทธ๋๋ฐ์ ๋ง์น ์ค์ ์์ ๊ธฐ๋ค๋ฆฌ๋ ๊ฒ๊ณผ ๊ฐ์. ๐ถโโ๏ธ๐ถโโ๏ธ๐ถ ํ ์์
์ด ๋๋์ผ ๋ค์ ์์
์ ์์ํ ์ ์์ง. ์ปคํผ์์์ ์ฃผ๋ฌธํ๊ณ ์ปคํผ ๋ฐ์ ๋๊น์ง ๊ผผ์ง์์ด ๊ธฐ๋ค๋ ค์ผ ํ๋ ์ํฉ์ด๋๊น?
๋ฐ๋ฉด ๋น๋๊ธฐ(Asynchronous) ํ๋ก๊ทธ๋๋ฐ์ ์ฃผ๋ฌธ๋ง ํ๊ณ ์ง๋๋ฒจ ๋ฐ์์ ์๋ฆฌ๋ก ๊ฐ๋ ๊ฑฐ์ผ. โ ์ปคํผ๊ฐ ์ค๋น๋๋ฉด ์๋ ค์ฃผ๋๊น ๊ทธ๋์ ๋ค๋ฅธ ์ผ์ ํ ์ ์์ง! ์ด๊ฒ ๋ฐ๋ก ๋น๋๊ธฐ์ ํต์ฌ์ด์ผ.
๐ ์ค์ํ ๋น์ ๋ก ์ดํดํ๊ธฐ
๋๊ธฐ ๋ฐฉ์:
1. ๋นจ๋๋ฅผ ๋๋ฆฐ๋ค (30๋ถ ์์) โ ๋นจ๋๊ฐ ๋๋ ๋๊น์ง ์ธํ๊ธฐ ์์์ ๋๊ธฐ ๐ด
2. ๋นจ๋๊ฐ ๋๋๋ฉด ์ค๊ฑฐ์ง๋ฅผ ์์ (20๋ถ ์์) โ ์ค๊ฑฐ์ง๊ฐ ๋๋ ๋๊น์ง ๋๊ธฐ
3. ์ค๊ฑฐ์ง๊ฐ ๋๋๋ฉด ์ฒญ์ ์์
๋น๋๊ธฐ ๋ฐฉ์:
1. ๋นจ๋๋ฅผ ๋๋ฆฐ๋ค (30๋ถ ์์) โ ๋นจ๋ ๋์๊ฐ๋ ๋์ ์ค๊ฑฐ์ง ์์ ๐
2. ์ค๊ฑฐ์งํ๋ฉด์ ์ฒญ์๋ ์กฐ๊ธ์ฉ ์งํ
3. ๋นจ๋ ๋๋๋ฉด ์๋ฆผ ๋ฐ๊ณ ๋นจ๋ ๊บผ๋ด๊ธฐ
๊ฐ์ ์๊ฐ์ ํจ์ฌ ๋ง์ ์ผ์ ํ ์ ์์ง? ์ด๊ฒ ๋ฐ๋ก ๋น๋๊ธฐ์ ํ์ด์ผ!
โก async์ await์ ๋ง๋ฒ
C# 5.0๋ถํฐ ๋ฑ์ฅํ async์ await ํค์๋๋ ์ ๋ง ํ๋ช
์ ์ด์์ด! ์ด์ ์๋ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ด ์ฝ๋ฐฑ ์ง์ฅ(Callback Hell)์ด๋ผ๊ณ ๋ถ๋ฆด ๋งํผ ๋ณต์กํ๊ฑฐ๋ . ๐ต
ํ์ง๋ง async/await ๋๋ถ์ ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋ง์น ๋๊ธฐ ์ฝ๋์ฒ๋ผ ์ง๊ด์ ์ผ๋ก ์์ฑํ ์ ์๊ฒ ๋์ง. ์ด๊ฒ ์ผ๋ง๋ ๋๋จํ ๊ฑด์ง ์ฝ๋๋ก ์ง์ ๋ณด์ฌ์ค๊ฒ!
๐ ๊ธฐ๋ณธ ๋ฌธ๋ฒ ๊ตฌ์กฐ
async/await์ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ ์ ๋ง ๊ฐ๋จํด:
// async ํค์๋๋ก ๋น๋๊ธฐ ๋ฉ์๋์์ ์ ์ธ
public async Task<string> GetDataAsync()
{
// await ํค์๋๋ก ๋น๋๊ธฐ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๋๊ธฐ
string result = await SomeAsyncOperation();
return result;
}
์ฌ๊ธฐ์ ํต์ฌ์:
โ
async: ์ด ๋ฉ์๋๊ฐ ๋น๋๊ธฐ ๋ฉ์๋๋ผ๊ณ ์ปดํ์ผ๋ฌ์๊ฒ ์๋ ค์ค
โ
await: ๋น๋๊ธฐ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋, ์ค๋ ๋๋ ๋ธ๋กํนํ์ง ์์
โ
Task: ๋น๋๊ธฐ ์์
์ ๋ํ๋ด๋ ๊ฐ์ฒด (๋ฐํ๊ฐ์ด ์์ผ๋ฉด Task<T>)
๐ฌ ์ค์ ์์ : ์น ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
์ค์ ๋ก ๋ง์ด ์ฌ์ฉํ๋ ์๋๋ฆฌ์ค๋ฅผ ๋ณด์. ์น API์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ์ผ:
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class DataService
{
private static readonly HttpClient client = new HttpClient();
// ๋๊ธฐ ๋ฐฉ์ (๋์ ์์ โ)
public string GetDataSync(string url)
{
// ์ด๋ ๊ฒ ํ๋ฉด UI๊ฐ ๋ฉ์ถฐ๋ฒ๋ ค!
var response = client.GetStringAsync(url).Result;
return response;
}
// ๋น๋๊ธฐ ๋ฐฉ์ (์ข์ ์์ โ
)
public async Task<string> GetDataAsync(string url)
{
try
{
// await๋ฅผ ์ฌ์ฉํ๋ฉด UI๋ ๊ณ์ ๋ฐ์ํด!
string response = await client.GetStringAsync(url);
// ๋ฐ์ดํฐ ์ฒ๋ฆฌ
string processed = ProcessData(response);
return processed;
}
catch (HttpRequestException ex)
{
Console.WriteLine($"์ค๋ฅ ๋ฐ์: {ex.Message}");
return null;
}
}
private string ProcessData(string data)
{
// ๋ฐ์ดํฐ ๊ฐ๊ณต ๋ก์ง
return data.ToUpper();
}
}
// ์ฌ์ฉ ์์
public async Task UseDataService()
{
var service = new DataService();
// ์ฌ๋ฌ ์์ฒญ์ ๋์์ ์ฒ๋ฆฌํ ์ ์์ด!
var task1 = service.GetDataAsync("https://api.example.com/data1");
var task2 = service.GetDataAsync("https://api.example.com/data2");
var task3 = service.GetDataAsync("https://api.example.com/data3");
// ๋ชจ๋ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๋๊ธฐ
await Task.WhenAll(task1, task2, task3);
Console.WriteLine($"๊ฒฐ๊ณผ1: {task1.Result}");
Console.WriteLine($"๊ฒฐ๊ณผ2: {task2.Result}");
Console.WriteLine($"๊ฒฐ๊ณผ3: {task3.Result}");
}
์ ์ฝ๋์์ ์ฃผ๋ชฉํ ์ ์ Task.WhenAll์ด์ผ! ์ด๊ฑธ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ๋น๋๊ธฐ ์์ ์ ๋์์ ์คํํ๊ณ , ๋ชจ๋ ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆด ์ ์์ด. ์์ฐจ์ ์ผ๋ก ์คํํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ๋น ๋ฅด์ง! ๐
๐ญ Task์ ๋ชจ๋ ๊ฒ
Task๋ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ ํด๋์ค์ผ. .NET Framework 4.0๋ถํฐ ๋์
๋ Task๋ ๋น๋๊ธฐ ์์
์ ํํํ๋ ๊ฐ์ฒด๋ก, ์์
์ ์ํ, ๊ฒฐ๊ณผ, ์์ธ ๋ฑ์ ๊ด๋ฆฌํด์ค.
Task๋ฅผ ์ ๋๋ก ์ดํดํ๋ฉด ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ 90%๋ ๋ง์คํฐํ ๊ฑฐ๋ ๋ค๋ฆ์์ด! ๐ช
๐ง Task์ ์ฃผ์ ํน์ง
| ํน์ง | ์ค๋ช | ์์ |
|---|---|---|
| Task | ๋ฐํ๊ฐ์ด ์๋ ๋น๋๊ธฐ ์์ | Task.Run(() => DoWork()) |
| Task<T> | T ํ์ ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ๋น๋๊ธฐ ์์ | Task<int> GetNumberAsync() |
| Task.Status | ์์ ์ ํ์ฌ ์ํ (Created, Running, Completed ๋ฑ) | if (task.Status == TaskStatus.Running) |
| Task.Result | ์์ ์ ๊ฒฐ๊ณผ๊ฐ (๋ธ๋กํน ๋ฐ์ ์ฃผ์!) | int result = task.Result; |
| Task.Exception | ์์ ์ค ๋ฐ์ํ ์์ธ | if (task.Exception != null) |
๐จ Task ์์ฑ ๋ฐฉ๋ฒ๋ค
Task๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ด. ์ํฉ์ ๋ง๊ฒ ์ ํํ๋ฉด ๋ผ:
// 1. Task.Run - CPU ์ง์ฝ์ ์์
์ ์ ํฉ
Task<int> task1 = Task.Run(() =>
{
// ๋ณต์กํ ๊ณ์ฐ ์์
int sum = 0;
for (int i = 0; i < 1000000; i++)
{
sum += i;
}
return sum;
});
// 2. Task.Factory.StartNew - ๋ ์ธ๋ฐํ ์ ์ด๊ฐ ํ์ํ ๋
Task<int> task2 = Task.Factory.StartNew(() =>
{
return 42;
}, TaskCreationOptions.LongRunning); // ์ฅ์๊ฐ ์คํ๋๋ ์์
์์ ๋ช
์
// 3. async ๋ฉ์๋ - ๊ฐ์ฅ ๊ถ์ฅ๋๋ ๋ฐฉ์
public async Task<int> CalculateAsync()
{
await Task.Delay(1000); // 1์ด ๋๊ธฐ
return 100;
}
// 4. Task.FromResult - ์ด๋ฏธ ๊ฒฐ๊ณผ๊ฐ ์์ ๋
Task<int> task3 = Task.FromResult(42);
// 5. TaskCompletionSource - ์๋์ผ๋ก Task ์ ์ด
var tcs = new TaskCompletionSource<int>();
Task<int> task4 = tcs.Task;
// ๋์ค์ ๊ฒฐ๊ณผ ์ค์
tcs.SetResult(100);
๐ฏ Task ์กฐํฉ ๋ฉ์๋๋ค
์ฌ๋ฌ Task๋ฅผ ์กฐํฉํด์ ์ฌ์ฉํ๋ ๊ฐ๋ ฅํ ๋ฉ์๋๋ค์ด ์์ด:
public async Task DemoTaskCombinations()
{
// Task.WhenAll - ๋ชจ๋ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๋๊ธฐ
var tasks = new[]
{
DownloadAsync("url1"),
DownloadAsync("url2"),
DownloadAsync("url3")
};
string[] results = await Task.WhenAll(tasks);
Console.WriteLine($"๋ชจ๋ ๋ค์ด๋ก๋ ์๋ฃ! ์ด {results.Length}๊ฐ");
// Task.WhenAny - ํ๋๋ผ๋ ์๋ฃ๋๋ฉด ์งํ
Task<string> firstCompleted = await Task.WhenAny(tasks);
Console.WriteLine($"๊ฐ์ฅ ๋น ๋ฅธ ๊ฒฐ๊ณผ: {await firstCompleted}");
// Task.Delay - ๋น๋๊ธฐ ๋๊ธฐ (Thread.Sleep๋ณด๋ค ํจ์ฌ ์ข์!)
await Task.Delay(TimeSpan.FromSeconds(2));
Console.WriteLine("2์ด ๊ฒฝ๊ณผ!");
// Task.FromException - ์์ธ๋ฅผ ํฌํจํ Task ์์ฑ
Task<int> faultyTask = Task.FromException<int>(
new InvalidOperationException("์๋์ ์ค๋ฅ")
);
// Task.FromCanceled - ์ทจ์๋ Task ์์ฑ
var cts = new CancellationTokenSource();
cts.Cancel();
Task canceledTask = Task.FromCanceled(cts.Token);
}
private async Task<string> DownloadAsync(string url)
{
await Task.Delay(Random.Shared.Next(1000, 3000));
return $"Downloaded from {url}";
}
๐ช ์ค์ ํจํด๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
์ด๋ก ์ ์ถฉ๋ถํ ๋ฐฐ์ ์ผ๋ ์ด์ ์ค์ ์์ ์ด๋ป๊ฒ ํ์ฉํ๋์ง ์์๋ณด์! ์ค๋ฌด์์ ์์ฃผ ๋ง์ฃผ์น๋ ์๋๋ฆฌ์ค๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์๊ฐํ ๊ฒ. ๐
๐ฏ ํจํด 1: ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ก ์ฑ๋ฅ ๊ทน๋ํ
์ฌ๋ฌ ๋ ๋ฆฝ์ ์ธ ์์ ์ ๋์์ ์ฒ๋ฆฌํ๋ฉด ์์ฒญ๋ ์ฑ๋ฅ ํฅ์์ ์ป์ ์ ์์ด:
public class ParallelProcessingExample
{
// ๋์ ์: ์์ฐจ ์ฒ๋ฆฌ (๋๋ฆผ ๐)
public async Task<List<UserData>> GetUserDataSequential(List<int> userIds)
{
var results = new List<UserData>();
foreach (var userId in userIds)
{
var userData = await FetchUserDataAsync(userId); // ํ๋์ฉ ๋๊ธฐ
results.Add(userData);
}
return results; // 10๋ช
์ด๋ฉด 10์ด ๊ฑธ๋ฆผ
}
// ์ข์ ์: ๋ณ๋ ฌ ์ฒ๋ฆฌ (๋น ๋ฆ ๐)
public async Task<List<UserData>> GetUserDataParallel(List<int> userIds)
{
// ๋ชจ๋ ์์ฒญ์ ๋์์ ์์
var tasks = userIds.Select(id => FetchUserDataAsync(id));
// ๋ชจ๋ ์์
์ด ์๋ฃ๋ ๋๊น์ง ๋๊ธฐ
var results = await Task.WhenAll(tasks);
return results.ToList(); // 10๋ช
์ด์ด๋ 1~2์ด๋ฉด ๋!
}
// ๋ ๋์ ์: ๋์ ์คํ ์ ์ ํ (์๋ฒ ๋ถํ ๊ณ ๋ ค)
public async Task<List<UserData>> GetUserDataThrottled(
List<int> userIds,
int maxConcurrency = 5)
{
var results = new List<UserData>();
var semaphore = new SemaphoreSlim(maxConcurrency);
var tasks = userIds.Select(async userId =>
{
await semaphore.WaitAsync(); // ๋์ ์คํ ์ ์ ํ
try
{
return await FetchUserDataAsync(userId);
}
finally
{
semaphore.Release();
}
});
return (await Task.WhenAll(tasks)).ToList();
}
private async Task<UserData> FetchUserDataAsync(int userId)
{
await Task.Delay(1000); // API ํธ์ถ ์๋ฎฌ๋ ์ด์
return new UserData { Id = userId, Name = $"User{userId}" };
}
}
public class UserData
{
public int Id { get; set; }
public string Name { get; set; }
}
๐ฏ ํจํด 2: ์ทจ์ ํ ํฐ์ผ๋ก ์์ ์ ์ดํ๊ธฐ
์ฌ์ฉ์๊ฐ ์์ ์ ์ทจ์ํ๊ณ ์ถ์ ๋๊ฐ ์์ง? ์๋ฅผ ๋ค์ด ํ์ผ ๋ค์ด๋ก๋ ์ค์ "์ทจ์" ๋ฒํผ์ ๋๋ฅด๋ ๊ฒฝ์ฐ! ์ด๋ด ๋ CancellationToken์ ์ฌ์ฉํด:
public class CancellationExample
{
// ์ทจ์ ๊ฐ๋ฅํ ๋ค์ด๋ก๋ ๋ฉ์๋
public async Task<byte[]> DownloadFileAsync(
string url,
CancellationToken cancellationToken)
{
using var client = new HttpClient();
try
{
// ์ทจ์ ํ ํฐ์ ์ ๋ฌ
var response = await client.GetAsync(url, cancellationToken);
// ์ค๊ฐ์ ์ทจ์ ํ์ธ
cancellationToken.ThrowIfCancellationRequested();
return await response.Content.ReadAsByteArrayAsync();
}
catch (OperationCanceledException)
{
Console.WriteLine("๋ค์ด๋ก๋๊ฐ ์ทจ์๋์์ต๋๋ค.");
throw; // ์์๋ก ์ ํ
}
}
// ํ์์์์ด ์๋ ์์
public async Task<string> GetDataWithTimeout(string url, int timeoutSeconds)
{
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(timeoutSeconds));
try
{
return await DownloadFileAsync(url, cts.Token);
}
catch (OperationCanceledException)
{
throw new TimeoutException($"{timeoutSeconds}์ด ๋ด์ ์๋ฃ๋์ง ์์์ต๋๋ค.");
}
}
// ์ฌ์ฉ ์์
public async Task UseExample()
{
var cts = new CancellationTokenSource();
// 5์ด ํ ์๋ ์ทจ์
cts.CancelAfter(TimeSpan.FromSeconds(5));
// ๋๋ ๋ฒํผ ํด๋ฆญ ์ ์ทจ์
// cancelButton.Click += (s, e) => cts.Cancel();
try
{
var data = await DownloadFileAsync(
"https://example.com/largefile.zip",
cts.Token
);
Console.WriteLine($"๋ค์ด๋ก๋ ์๋ฃ: {data.Length} bytes");
}
catch (OperationCanceledException)
{
Console.WriteLine("์ฌ์ฉ์๊ฐ ์ทจ์ํ๊ฑฐ๋ ํ์์์ ๋ฐ์!");
}
}
}
๐ฏ ํจํด 3: ์์ธ ์ฒ๋ฆฌ ์ ๋ต
๋น๋๊ธฐ ์ฝ๋์์ ์์ธ ์ฒ๋ฆฌ๋ ํน๋ณํ ์ ๊ฒฝ ์จ์ผ ํด! ์๋ชปํ๋ฉด ์์ธ๊ฐ ์กฐ์ฉํ ์ฌ๋ผ์ง ์ ์๊ฑฐ๋ :
public class ExceptionHandlingExample
{
// ๊ธฐ๋ณธ ์์ธ ์ฒ๋ฆฌ
public async Task<string> SafeApiCall(string url)
{
try
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
}
catch (HttpRequestException ex)
{
// ๋คํธ์ํฌ ์ค๋ฅ
Console.WriteLine($"๋คํธ์ํฌ ์ค๋ฅ: {ex.Message}");
return null;
}
catch (TaskCanceledException ex)
{
// ํ์์์
Console.WriteLine($"์์ฒญ ์๊ฐ ์ด๊ณผ: {ex.Message}");
return null;
}
catch (Exception ex)
{
// ๊ธฐํ ์์ธ
Console.WriteLine($"์์์น ๋ชปํ ์ค๋ฅ: {ex.Message}");
throw; // ์ฌ๊ฐํ ์ค๋ฅ๋ ์์๋ก ์ ํ
}
}
// ์ฌ๋ฌ ์์
์ ์์ธ ์ฒ๋ฆฌ
public async Task<List<string>> MultipleApiCallsWithErrorHandling(
List<string> urls)
{
var tasks = urls.Select(async url =>
{
try
{
return await SafeApiCall(url);
}
catch (Exception ex)
{
Console.WriteLine($"{url} ์คํจ: {ex.Message}");
return null; // ์คํจํ ๊ฒ๋ง null๋ก ๋ฐํ
}
});
var results = await Task.WhenAll(tasks);
// null์ด ์๋ ๊ฒ๋ง ๋ฐํ
return results.Where(r => r != null).ToList();
}
// AggregateException ์ฒ๋ฆฌ
public async Task HandleAggregateException()
{
var tasks = new[]
{
Task.Run(() => throw new InvalidOperationException("์ค๋ฅ1")),
Task.Run(() => throw new ArgumentException("์ค๋ฅ2")),
Task.Run(() => throw new NullReferenceException("์ค๋ฅ3"))
};
try
{
await Task.WhenAll(tasks);
}
catch (Exception ex)
{
// Task.WhenAll์ ์ฒซ ๋ฒ์งธ ์์ธ๋ง ๋์ง
Console.WriteLine($"์ฒซ ๋ฒ์งธ ์์ธ: {ex.Message}");
// ๋ชจ๋ ์์ธ๋ฅผ ํ์ธํ๋ ค๋ฉด
foreach (var task in tasks)
{
if (task.Exception != null)
{
foreach (var innerEx in task.Exception.InnerExceptions)
{
Console.WriteLine($"์์ธ ๋ฐ๊ฒฌ: {innerEx.Message}");
}
}
}
}
}
}
๐๏ธ ๊ณ ๊ธ ํจํด๊ณผ ์ต์ ํ ๊ธฐ๋ฒ
์ด์ ์ข ๋ ๊ณ ๊ธ ๊ธฐ๋ฒ๋ค์ ์์๋ณด์! ์ด๋ฐ ํจํด๋ค์ ์๋ฉด ์ง์ง ํ๋ก ๊ฐ๋ฐ์์ฒ๋ผ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ด. ๐
๐ฏ ValueTask๋ก ์ฑ๋ฅ ์ต์ ํ
ValueTask<T>๋ Task<T>๋ณด๋ค ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ด์ผ. ํนํ ๊ฒฐ๊ณผ๊ฐ ์ด๋ฏธ ์ค๋น๋์ด ์๊ฑฐ๋ ๋๊ธฐ์ ์ผ๋ก ์๋ฃ๋๋ ๊ฒฝ์ฐ์ ์ ์ฉํด:
public class ValueTaskExample
{
private Dictionary<string, string> _cache = new();
// Task ๋ฒ์ - ํญ์ ํ ํ ๋น ๋ฐ์
public async Task<string> GetDataWithTask(string key)
{
if (_cache.TryGetValue(key, out var cached))
{
return cached; // ์บ์ ํํธ์ฌ๋ Task ๊ฐ์ฒด ์์ฑ
}
var data = await FetchFromDatabaseAsync(key);
_cache[key] = data;
return data;
}
// ValueTask ๋ฒ์ - ์บ์ ํํธ ์ ํ ๋น ์์! ๐
public async ValueTask<string> GetDataWithValueTask(string key)
{
if (_cache.TryGetValue(key, out var cached))
{
return cached; // ํ ํ ๋น ์์ด ๋ฐ๋ก ๋ฐํ!
}
var data = await FetchFromDatabaseAsync(key);
_cache[key] = data;
return data;
}
private async Task<string> FetchFromDatabaseAsync(string key)
{
await Task.Delay(100); // DB ์กฐํ ์๋ฎฌ๋ ์ด์
return $"Data for {key}";
}
}
// ์ฑ๋ฅ ๋น๊ต
public class PerformanceComparison
{
public async Task ComparePerformance()
{
var example = new ValueTaskExample();
// ์ฒซ ํธ์ถ - ๋ ๋ค ๋น์ท
await example.GetDataWithTask("key1");
await example.GetDataWithValueTask("key1");
// ์บ์๋ ๋ฐ์ดํฐ ๋ฐ๋ณต ํธ์ถ - ValueTask๊ฐ ํจ์ฌ ๋น ๋ฆ!
for (int i = 0; i < 1000000; i++)
{
await example.GetDataWithValueTask("key1"); // ๋ฉ๋ชจ๋ฆฌ ํ ๋น ๊ฑฐ์ ์์
}
}
}
โ ๋๊ธฐ์ ์ผ๋ก ์๋ฃ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ ๊ฒฝ์ฐ
โ ๊ณ ์ฑ๋ฅ์ด ์ค์ํ ํซํจ์ค(hot path)
โ ์ฌ๋ฌ ๋ฒ awaitํ๋ ๊ฒฝ์ฐ (ValueTask๋ ํ ๋ฒ๋ง await ๊ฐ๋ฅ!)
โ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํด์ผ ํ๋ ๊ฒฝ์ฐ
๐ฏ ConfigureAwait๋ก ์ปจํ ์คํธ ์ ์ด
ConfigureAwait(false)๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฝ๋์์ ์ฑ๋ฅ์ ๋์ด๋ ๋น๋ฐ ๋ฌด๊ธฐ์ผ:
public class ConfigureAwaitExample
{
// UI ์ ํ๋ฆฌ์ผ์ด์
์์ (WPF, WinForms, etc.)
public async Task UpdateUIAsync()
{
var data = await GetDataAsync(); // UI ์ค๋ ๋๋ก ๋์์ด
// UI ์
๋ฐ์ดํธ ๊ฐ๋ฅ
// textBox.Text = data;
}
// ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฝ๋ - ConfigureAwait(false) ์ฌ์ฉ
public async Task<string> GetDataAsync()
{
using var client = new HttpClient();
// ConfigureAwait(false) - UI ์ค๋ ๋๋ก ๋์๊ฐ ํ์ ์์
var response = await client.GetStringAsync("https://api.example.com")
.ConfigureAwait(false);
// ์ฌ๊ธฐ์๋ UI ์ ๊ทผ ๋ถ๊ฐ! (ํ์ง๋ง ๋ ๋น ๋ฆ)
var processed = ProcessData(response);
return processed;
}
private string ProcessData(string data)
{
// ์์ ๊ณ์ฐ ๋ก์ง
return data.ToUpper();
}
}
// ์ค์ ์์: ASP.NET Core์์
public class ApiController
{
private readonly HttpClient _httpClient;
public async Task<IActionResult> GetUserData(int userId)
{
// ASP.NET Core๋ SynchronizationContext๊ฐ ์์ผ๋ฏ๋ก
// ConfigureAwait(false)๊ฐ ํฐ ์๋ฏธ๋ ์์ง๋ง
// ์ต๊ด์ ์ผ๋ก ์ฌ์ฉํ๋ฉด ์ข์
var userData = await FetchUserAsync(userId)
.ConfigureAwait(false);
return Ok(userData);
}
private async Task<User> FetchUserAsync(int userId)
{
var response = await _httpClient
.GetStringAsync($"https://api.example.com/users/{userId}")
.ConfigureAwait(false);
return JsonSerializer.Deserialize<User>(response);
}
}
๐ฏ IAsyncEnumerable๋ก ์คํธ๋ฆฌ๋ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
C# 8.0๋ถํฐ ๋์ ๋ IAsyncEnumerable<T>๋ ๋น๋๊ธฐ ์คํธ๋ฆผ์ ์ฒ๋ฆฌํ ๋ ์ ๋ง ์ ์ฉํด:
public class AsyncEnumerableExample
{
// ์ ํต์ ์ธ ๋ฐฉ์ - ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋
public async Task<List<string>> GetAllLogsOldWay()
{
var logs = new List<string>();
for (int i = 0; i < 10000; i++)
{
var log = await FetchLogAsync(i);
logs.Add(log);
}
return logs; // ๋ฉ๋ชจ๋ฆฌ ๋ง์ด ์ฌ์ฉ! ๐ฐ
}
// ์๋ก์ด ๋ฐฉ์ - ์คํธ๋ฆฌ๋ฐ์ผ๋ก ์ฒ๋ฆฌ
public async IAsyncEnumerable<string> GetAllLogsNewWay()
{
for (int i = 0; i < 10000; i++)
{
var log = await FetchLogAsync(i);
yield return log; // ํ๋์ฉ ๋ฐํ! ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ! ๐
}
}
// ์ฌ์ฉ ์์
public async Task ProcessLogs()
{
await foreach (var log in GetAllLogsNewWay())
{
// ๋ก๊ทธ๋ฅผ ํ๋์ฉ ์ฒ๋ฆฌ
Console.WriteLine(log);
// ํ์ํ๋ฉด ์ค๊ฐ์ ์ค๋จ ๊ฐ๋ฅ
if (log.Contains("ERROR"))
{
break;
}
}
}
// ์ค์ ์์: ๋์ฉ๋ ํ์ผ ์ฝ๊ธฐ
public async IAsyncEnumerable<string> ReadLargeFileAsync(
string filePath,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
using var reader = new StreamReader(filePath);
while (!reader.EndOfStream)
{
cancellationToken.ThrowIfCancellationRequested();
var line = await reader.ReadLineAsync();
if (line != null)
{
yield return line;
}
}
}
// LINQ์ ํจ๊ป ์ฌ์ฉ
public async Task UseWithLinq()
{
var errorLogs = GetAllLogsNewWay()
.Where(log => log.Contains("ERROR"))
.Take(10);
await foreach (var log in errorLogs)
{
Console.WriteLine(log);
}
}
private async Task<string> FetchLogAsync(int index)
{
await Task.Delay(10);
return $"Log entry {index}";
}
}
๐ IAsyncEnumerable์ ์ฅ์
1. ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ ๐พ
์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ์ง ์๊ณ ํ์ํ ๋งํผ๋ง ์ฒ๋ฆฌ
2. ์๋ต์ฑ ํฅ์ โก
์ฒซ ๋ฒ์งธ ํญ๋ชฉ์ ์ฆ์ ์ฒ๋ฆฌํ ์ ์์ด ์ฌ์ฉ์ ๊ฒฝํ ๊ฐ์
3. ์ทจ์ ์ง์ ๐
CancellationToken์ผ๋ก ์ธ์ ๋ ์ค๋จ ๊ฐ๋ฅ
4. ๋ฌดํ ์คํธ๋ฆผ ์ง์ โพ๏ธ
๋์ด ์๋ ๋ฐ์ดํฐ ์คํธ๋ฆผ๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ (์: ์ค์๊ฐ ์ผ์ ๋ฐ์ดํฐ)
๐ฎ ์ค์ ํ๋ก์ ํธ ์์
์ด๋ก ๋ง ๋ฐฐ์ฐ๋ฉด ์ฌ๋ฏธ์์ง! ์ค์ ๋ก ๋์ํ๋ ์์ ํ ์์ ๋ฅผ ๋ง๋ค์ด๋ณด์. ์ฌ๊ธฐ์๋ ๋ ์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ๊ฑฐ์ผ. ๐ค๏ธ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
// ๋ ์จ ๋ฐ์ดํฐ ๋ชจ๋ธ
public class WeatherData
{
public string City { get; set; }
public double Temperature { get; set; }
public string Description { get; set; }
public DateTime Timestamp { get; set; }
}
// ๋ ์จ ์๋น์ค ์ธํฐํ์ด์ค
public interface IWeatherService
{
Task<WeatherData> GetWeatherAsync(string city, CancellationToken ct = default);
Task<List<WeatherData>> GetMultipleCitiesWeatherAsync(
List<string> cities,
CancellationToken ct = default);
IAsyncEnumerable<WeatherData> StreamWeatherUpdatesAsync(
string city,
TimeSpan interval,
CancellationToken ct = default);
}
// ๋ ์จ ์๋น์ค ๊ตฌํ
public class WeatherService : IWeatherService
{
private readonly HttpClient _httpClient;
private readonly Dictionary<string, WeatherData> _cache;
private readonly SemaphoreSlim _cacheLock;
private readonly TimeSpan _cacheExpiration;
public WeatherService(HttpClient httpClient)
{
_httpClient = httpClient;
_cache = new Dictionary<string, WeatherData>();
_cacheLock = new SemaphoreSlim(1, 1);
_cacheExpiration = TimeSpan.FromMinutes(5);
}
// ๋จ์ผ ๋์ ๋ ์จ ์กฐํ (์บ์ฑ ํฌํจ)
public async Task<WeatherData> GetWeatherAsync(
string city,
CancellationToken ct = default)
{
// ์บ์ ํ์ธ
await _cacheLock.WaitAsync(ct);
try
{
if (_cache.TryGetValue(city, out var cached))
{
if (DateTime.Now - cached.Timestamp < _cacheExpiration)
{
Console.WriteLine($"โ
์บ์์์ {city} ๋ ์จ ๋ฐํ");
return cached;
}
}
}
finally
{
_cacheLock.Release();
}
// API ํธ์ถ
Console.WriteLine($"๐ {city} ๋ ์จ API ํธ์ถ ์ค...");
try
{
// ์ค์ ๋ก๋ OpenWeatherMap ๊ฐ์ API ์ฌ์ฉ
// ์ฌ๊ธฐ์๋ ์๋ฎฌ๋ ์ด์
await Task.Delay(Random.Shared.Next(500, 1500), ct);
var weather = new WeatherData
{
City = city,
Temperature = Random.Shared.Next(-10, 35),
Description = GetRandomWeather(),
Timestamp = DateTime.Now
};
// ์บ์ ์
๋ฐ์ดํธ
await _cacheLock.WaitAsync(ct);
try
{
_cache[city] = weather;
}
finally
{
_cacheLock.Release();
}
return weather;
}
catch (OperationCanceledException)
{
Console.WriteLine($"โ {city} ๋ ์จ ์กฐํ ์ทจ์๋จ");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"โ {city} ๋ ์จ ์กฐํ ์คํจ: {ex.Message}");
throw;
}
}
// ์ฌ๋ฌ ๋์ ๋ ์จ ๋ณ๋ ฌ ์กฐํ
public async Task<List<WeatherData>> GetMultipleCitiesWeatherAsync(
List<string> cities,
CancellationToken ct = default)
{
Console.WriteLine($"๐ {cities.Count}๊ฐ ๋์ ๋ ์จ ์กฐํ ์์...");
var tasks = cities.Select(city => GetWeatherAsync(city, ct));
try
{
var results = await Task.WhenAll(tasks);
Console.WriteLine($"โ
๋ชจ๋ ๋์ ๋ ์จ ์กฐํ ์๋ฃ!");
return results.ToList();
}
catch (Exception ex)
{
Console.WriteLine($"โ ์ผ๋ถ ๋์ ์กฐํ ์คํจ: {ex.Message}");
// ์ฑ๊ณตํ ๊ฒ๋ง ๋ฐํ
var successfulResults = tasks
.Where(t => t.IsCompletedSuccessfully)
.Select(t => t.Result)
.ToList();
return successfulResults;
}
}
// ์ค์๊ฐ ๋ ์จ ์
๋ฐ์ดํธ ์คํธ๋ฆผ
public async IAsyncEnumerable<WeatherData> StreamWeatherUpdatesAsync(
string city,
TimeSpan interval,
[EnumeratorCancellation] CancellationToken ct = default)
{
Console.WriteLine($"๐ก {city} ์ค์๊ฐ ๋ ์จ ์คํธ๋ฆฌ๋ฐ ์์...");
while (!ct.IsCancellationRequested)
{
WeatherData weather;
try
{
weather = await GetWeatherAsync(city, ct);
yield return weather;
}
catch (OperationCanceledException)
{
Console.WriteLine($"๐ {city} ์คํธ๋ฆฌ๋ฐ ์ค๋จ๋จ");
yield break;
}
try
{
await Task.Delay(interval, ct);
}
catch (OperationCanceledException)
{
yield break;
}
}
}
private string GetRandomWeather()
{
var conditions = new[] { "๋ง์", "ํ๋ฆผ", "๋น", "๋", "์๊ฐ" };
return conditions[Random.Shared.Next(conditions.Length)];
}
}
// ์ฌ์ฉ ์์
public class WeatherApp
{
private readonly IWeatherService _weatherService;
public WeatherApp(IWeatherService weatherService)
{
_weatherService = weatherService;
}
// ์์ 1: ๋จ์ผ ๋์ ์กฐํ
public async Task Example1_SingleCity()
{
Console.WriteLine("\n=== ์์ 1: ๋จ์ผ ๋์ ์กฐํ ===");
var weather = await _weatherService.GetWeatherAsync("์์ธ");
PrintWeather(weather);
}
// ์์ 2: ์ฌ๋ฌ ๋์ ๋ณ๋ ฌ ์กฐํ
public async Task Example2_MultipleCities()
{
Console.WriteLine("\n=== ์์ 2: ์ฌ๋ฌ ๋์ ๋ณ๋ ฌ ์กฐํ ===");
var cities = new List<string>
{
"์์ธ", "๋ถ์ฐ", "๋๊ตฌ", "์ธ์ฒ", "๊ด์ฃผ",
"๋์ ", "์ธ์ฐ", "์ ์ฃผ", "์์", "์ฐฝ์"
};
var startTime = DateTime.Now;
var results = await _weatherService.GetMultipleCitiesWeatherAsync(cities);
var elapsed = DateTime.Now - startTime;
Console.WriteLine($"\nโฑ๏ธ ์ด ์์ ์๊ฐ: {elapsed.TotalSeconds:F2}์ด");
Console.WriteLine($"๐ ์ฑ๊ณต: {results.Count}/{cities.Count}๊ฐ ๋์\n");
foreach (var weather in results)
{
PrintWeather(weather);
}
}
// ์์ 3: ์ทจ์ ๊ฐ๋ฅํ ์กฐํ
public async Task Example3_WithCancellation()
{
Console.WriteLine("\n=== ์์ 3: ์ทจ์ ๊ฐ๋ฅํ ์กฐํ ===");
using var cts = new CancellationTokenSource();
// 2์ด ํ ์๋ ์ทจ์
cts.CancelAfter(TimeSpan.FromSeconds(2));
try
{
var cities = new List<string>
{
"์์ธ", "๋ถ์ฐ", "๋๊ตฌ", "์ธ์ฒ", "๊ด์ฃผ"
};
var results = await _weatherService.GetMultipleCitiesWeatherAsync(
cities,
cts.Token
);
Console.WriteLine($"โ
{results.Count}๊ฐ ๋์ ์กฐํ ์๋ฃ");
}
catch (OperationCanceledException)
{
Console.WriteLine("โฐ ์๊ฐ ์ด๊ณผ๋ก ์์
์ด ์ทจ์๋์์ต๋๋ค!");
}
}
// ์์ 4: ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ
public async Task Example4_Streaming()
{
Console.WriteLine("\n=== ์์ 4: ์ค์๊ฐ ๋ ์จ ์คํธ๋ฆฌ๋ฐ ===");
using var cts = new CancellationTokenSource();
// 10์ด ํ ์ค๋จ
cts.CancelAfter(TimeSpan.FromSeconds(10));
var updateCount = 0;
await foreach (var weather in _weatherService.StreamWeatherUpdatesAsync(
"์์ธ",
TimeSpan.FromSeconds(2),
cts.Token))
{
updateCount++;
Console.WriteLine($"\n[์
๋ฐ์ดํธ #{updateCount}]");
PrintWeather(weather);
}
Console.WriteLine($"\n์ด {updateCount}๋ฒ ์
๋ฐ์ดํธ๋จ");
}
// ์์ 5: ํ์์์ ์ฒ๋ฆฌ
public async Task Example5_WithTimeout()
{
Console.WriteLine("\n=== ์์ 5: ํ์์์ ์ฒ๋ฆฌ ===");
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(1));
try
{
var weather = await _weatherService.GetWeatherAsync("์์ธ", cts.Token);
PrintWeather(weather);
}
catch (OperationCanceledException)
{
Console.WriteLine("โฐ ์์ฒญ ์๊ฐ ์ด๊ณผ!");
}
}
private void PrintWeather(WeatherData weather)
{
Console.WriteLine($"๐ ๋์: {weather.City}");
Console.WriteLine($"๐ก๏ธ ์จ๋: {weather.Temperature}ยฐC");
Console.WriteLine($"โ๏ธ ๋ ์จ: {weather.Description}");
Console.WriteLine($"โฐ ์๊ฐ: {weather.Timestamp:HH:mm:ss}");
Console.WriteLine();
}
}
// ๋ฉ์ธ ํ๋ก๊ทธ๋จ
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("๐ค๏ธ ๋น๋๊ธฐ ๋ ์จ ์ ํ๋ฆฌ์ผ์ด์
์์!\n");
using var httpClient = new HttpClient();
var weatherService = new WeatherService(httpClient);
var app = new WeatherApp(weatherService);
try
{
// ๋ชจ๋ ์์ ์คํ
await app.Example1_SingleCity();
await app.Example2_MultipleCities();
await app.Example3_WithCancellation();
await app.Example4_Streaming();
await app.Example5_WithTimeout();
}
catch (Exception ex)
{
Console.WriteLine($"โ ์ค๋ฅ ๋ฐ์: {ex.Message}");
}
Console.WriteLine("\nโ
ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
๐ ์ด ์์ ์์ ๋ฐฐ์ด ํต์ฌ ๊ฐ๋ ๋ค
1. ์บ์ฑ๊ณผ ๋๊ธฐํ
SemaphoreSlim์ ์ฌ์ฉํ ์ค๋ ๋ ์์ ํ ์บ์ ๊ตฌํ
2. ๋ณ๋ ฌ ์ฒ๋ฆฌ
Task.WhenAll๋ก ์ฌ๋ฌ API ํธ์ถ์ ๋์์ ์ฒ๋ฆฌ
3. ์ทจ์ ํ ํฐ
CancellationToken์ผ๋ก ์์
์ทจ์ ๋ฐ ํ์์์ ๊ตฌํ
4. ๋น๋๊ธฐ ์คํธ๋ฆผ
IAsyncEnumerable๋ก ์ค์๊ฐ ๋ฐ์ดํฐ ์คํธ๋ฆฌ๋ฐ
5. ์์ธ ์ฒ๋ฆฌ
๋ถ๋ถ ์คํจ ์๋๋ฆฌ์ค ์ฒ๋ฆฌ ๋ฐ ์ฐ์ํ ๋ณต๊ตฌ
๐จ ํํ ์ค์์ ์ํฐํจํด
๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ค ๋ณด๋ฉด ๋๊ตฌ๋ ์ค์๋ฅผ ํด. ํ์ง๋ง ์ด๋ฐ ์ค์๋ค์ ๋ฏธ๋ฆฌ ์๊ณ ์์ผ๋ฉด ํผํ ์ ์์ง! ๐
โ ์ํฐํจํด 1: async void ๋จ์ฉ
// ๋์ ์ โ
public async void ProcessDataBad()
{
await Task.Delay(1000);
throw new Exception("์ค๋ฅ!"); // ์ด ์์ธ๋ ์ก์ ์ ์์ด!
}
// ์ข์ ์ โ
public async Task ProcessDataGood()
{
await Task.Delay(1000);
throw new Exception("์ค๋ฅ!"); // ์ด ์์ธ๋ ํธ์ถ์๊ฐ ์ฒ๋ฆฌ ๊ฐ๋ฅ
}
// async void๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ์์๋ง!
private async void Button_Click(object sender, EventArgs e)
{
try
{
await ProcessDataGood();
}
catch (Exception ex)
{
MessageBox.Show($"์ค๋ฅ: {ex.Message}");
}
}
โ ์ํฐํจํด 2: .Result๋ .Wait() ์ฌ์ฉ
// ๋์ ์ โ - ๋ฐ๋๋ฝ ๋ฐ์ ๊ฐ๋ฅ!
public string GetDataBad()
{
var task = GetDataAsync();
return task.Result; // UI ์ค๋ ๋์์ ์ด๋ฌ๋ฉด ๋ฉ์ถฐ๋ฒ๋ ค!
}
// ์ข์ ์ โ
public async Task<string> GetDataGood()
{
return await GetDataAsync();
}
// ์ ๋ง ๋๊ธฐ ๋ฉ์๋๊ฐ ํ์ํ๋ค๋ฉด
public string GetDataSync()
{
// GetAwaiter().GetResult()๊ฐ .Result๋ณด๋ค ๋ซ๊ธด ํ์ง๋ง
// ์ฌ์ ํ ๊ถ์ฅํ์ง ์์
return GetDataAsync().GetAwaiter().GetResult();
}
์ ๋ฐ๋๋ฝ์ด ๋ฐ์ํ ๊น?
1. UI ์ค๋ ๋๊ฐ .Result๋ฅผ ํธ์ถํ๋ฉฐ ๋ธ๋กํน๋จ
2. async ๋ฉ์๋๊ฐ ์๋ฃ๋๋ ค๋ฉด UI ์ค๋ ๋๋ก ๋์๊ฐ์ผ ํจ
3. ํ์ง๋ง UI ์ค๋ ๋๋ .Result๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ฉฐ ๋ธ๋กํน ์ค
4. ์๋ก ๊ธฐ๋ค๋ฆฌ๋ ๋ฐ๋๋ฝ ๋ฐ์! ๐
โ ์ํฐํจํด 3: async ๋ฉ์๋์์ ๋๊ธฐ ์ฝ๋ ํธ์ถ
// ๋์ ์ โ
public async Task<string> ReadFileBad(string path)
{
// File.ReadAllText๋ ๋๊ธฐ ๋ฉ์๋!
// ์ค๋ ๋๋ฅผ ๋ธ๋กํน์์ผ
return await Task.Run(() => File.ReadAllText(path));
}
// ์ข์ ์ โ
public async Task<string> ReadFileGood(string path)
{
// ์ง์ง ๋น๋๊ธฐ ๋ฉ์๋ ์ฌ์ฉ
return await File.ReadAllTextAsync(path);
}
โ ์ํฐํจํด 4: ๋ถํ์ํ async/await
// ๋์ ์ โ - ๋ถํ์ํ ์ํ ๋จธ์ ์์ฑ
public async Task<string> GetDataBad()
{
return await _httpClient.GetStringAsync("url");
}
// ์ข์ ์ โ
- Task๋ฅผ ๊ทธ๋๋ก ๋ฐํ
public Task<string> GetDataGood()
{
return _httpClient.GetStringAsync("url");
}
// ํ์ง๋ง ์์ธ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ฉด async/await ์ฌ์ฉ
public async Task<string> GetDataWithErrorHandling()
{
try
{
return await _httpClient.GetStringAsync("url");
}
catch (HttpRequestException ex)
{
// ์์ธ ์ฒ๋ฆฌ
return null;
}
}
โ ์ํฐํจํด 5: ๋น๋๊ธฐ ์์ฑ์
// ๋์ ์ โ - ์์ฑ์๋ async๊ฐ ๋ ์ ์์ด!
public class BadService
{
public BadService()
{
// ์ด๋ ๊ฒ ํ๋ฉด ์ ๋ผ!
InitializeAsync().Wait();
}
private async Task InitializeAsync()
{
await Task.Delay(1000);
}
}
// ์ข์ ์ โ
- ํฉํ ๋ฆฌ ํจํด ์ฌ์ฉ
public class GoodService
{
private GoodService()
{
// private ์์ฑ์
}
public static async Task<GoodService> CreateAsync()
{
var service = new GoodService();
await service.InitializeAsync();
return service;
}
private async Task InitializeAsync()
{
await Task.Delay(1000);
}
}
// ์ฌ์ฉ
var service = await GoodService.CreateAsync();
โ ์ํฐํจํด 6: ๋น๋๊ธฐ ๋ฉ์๋๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์คํ
// ๋์ ์ โ
public void ProcessItems(List<Item> items)
{
foreach (var item in items)
{
// ์์ฐจ์ ์ผ๋ก ์คํ - ๋๋ฆผ!
ProcessItemAsync(item).Wait();
}
}
// ์ข์ ์ โ
public async Task ProcessItems(List<Item> items)
{
// ๋ณ๋ ฌ๋ก ์คํ - ๋น ๋ฆ!
var tasks = items.Select(item => ProcessItemAsync(item));
await Task.WhenAll(tasks);
}
โ๏ธ ์ฑ๋ฅ ์ต์ ํ ํ
๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ์ง์ง ํ์ ์ฑ๋ฅ ์ต์ ํ์ ์์ด! ๋ช ๊ฐ์ง ํ์ ์๋ ค์ค๊ฒ. ๐
๐ ์ต์ ํ ํ 1: Task ์ฌ์ฌ์ฉ
public class TaskReusageExample
{
// ๋์ ์ โ - ๋งค๋ฒ ์๋ก์ด Task ์์ฑ
public Task<int> GetCachedValueBad()
{
return Task.FromResult(42); // ๋งค๋ฒ ํ ํ ๋น!
}
// ์ข์ ์ โ
- Task ์บ์ฑ
private static readonly Task<int> _cachedTask = Task.FromResult(42);
public Task<int> GetCachedValueGood()
{
return _cachedTask; // ์ฌ์ฌ์ฉ!
}
// ๋ ๋์ ์ โ
- ValueTask ์ฌ์ฉ
public ValueTask<int> GetCachedValueBest()
{
return new ValueTask<int>(42); // ํ ํ ๋น ์์!
}
}
๐ ์ต์ ํ ํ 2: ๋ฐฐ์น ์ฒ๋ฆฌ
public class BatchProcessingExample
{
// ๋์ ์ โ - ํ๋์ฉ ์ฒ๋ฆฌ
public async Task ProcessItemsOneByOne(List<int> items)
{
foreach (var item in items)
{
await ProcessAsync(item);
}
}
// ์ข์ ์ โ
- ๋ฐฐ์น๋ก ์ฒ๋ฆฌ
public async Task ProcessItemsInBatches(
List<int> items,
int batchSize = 10)
{
for (int i = 0; i < items.Count; i += batchSize)
{
var batch = items.Skip(i).Take(batchSize);
var tasks = batch.Select(item => ProcessAsync(item));
await Task.WhenAll(tasks);
}
}
private async Task ProcessAsync(int item)
{
await Task.Delay(100);
}
}
๐ ์ต์ ํ ํ 3: ์กฐ๊ธฐ ๋ฐํ
public class EarlyReturnExample
{
private Dictionary<string, string> _cache = new();
// ๋์ ์ โ
public async Task<string> GetDataBad(string key)
{
string result;
if (_cache.ContainsKey(key))
{
result = _cache[key];
}
else
{
result = await FetchDataAsync(key);
_cache[key] = result;
}
return result;
}
// ์ข์ ์ โ
- ์กฐ๊ธฐ ๋ฐํ์ผ๋ก ๋ถํ์ํ await ์ ๊ฑฐ
public async Task<string> GetDataGood(string key)
{
if (_cache.TryGetValue(key, out var cached))
{
return cached; // ๋๊ธฐ์ ์ผ๋ก ์ฆ์ ๋ฐํ
}
var result = await FetchDataAsync(key);
_cache[key] = result;
return result;
}
// ๋ ๋์ ์ โ
- ValueTask ์ฌ์ฉ
public async ValueTask<string> GetDataBest(string key)
{
if (_cache.TryGetValue(key, out var cached))
{
return cached; // ํ ํ ๋น ์์ด ๋ฐํ!
}
var result = await FetchDataAsync(key);
_cache[key] = result;
return result;
}
private async Task<string> FetchDataAsync(string key)
{
await Task.Delay(100);
return $"Data for {key}";
}
}
๐ ์ต์ ํ ํ 4: ์ ์ ํ ๋์์ฑ ์ ์ด
public class ConcurrencyControlExample
{
// ๋์ ์คํ ์ ์ ํ
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(5);
public async Task<List<Result>> ProcessWithLimit(List<Item> items)
{
var tasks = items.Select(async item =>
{
await _semaphore.WaitAsync();
try
{
return await ProcessItemAsync(item);
}
finally
{
_semaphore.Release();
}
});
return (await Task.WhenAll(tasks)).ToList();
}
// ๋๋ Parallel.ForEachAsync ์ฌ์ฉ (.NET 6+)
public async Task ProcessWithParallel(List<Item> items)
{
await Parallel.ForEachAsync(
items,
new ParallelOptions { MaxDegreeOfParallelism = 5 },
async (item, ct) =>
{
await ProcessItemAsync(item);
}
);
}
private async Task<Result> ProcessItemAsync(Item item)
{
await Task.Delay(100);
return new Result();
}
}
public class Item { }
public class Result { }
๐ฏ ์ค๋ฌด์์ ์์ฃผ ์ฐ๋ ํจํด
์ค์ ํ๋ก์ ํธ์์ ์ ๋ง ์ ์ฉํ ํจํด๋ค์ ์๊ฐํ ๊ฒ! ์ด๋ฐ ํจํด๋ค์ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ ๋ง์ด ์ฌ์ฉ๋๋ ๊ฒ์ฆ๋ ๋ฐฉ๋ฒ๋ค์ด์ผ. ๐ผ
๐จ ํจํด 1: Retry ํจํด
public class RetryPattern
{
// ์ฌ์๋ ๋ก์ง์ด ์๋ HTTP ํธ์ถ
public async Task<T> ExecuteWithRetry<T>(
Func<Task<T>> operation,
int maxRetries = 3,
TimeSpan? delay = null)
{
delay ??= TimeSpan.FromSeconds(1);
for (int i = 0; i < maxRetries; i++)
{
try
{
return await operation();
}
catch (Exception ex) when (i < maxRetries - 1)
{
Console.WriteLine($"์๋ {i + 1} ์คํจ: {ex.Message}");
Console.WriteLine($"{delay.Value.TotalSeconds}์ด ํ ์ฌ์๋...");
await Task.Delay(delay.Value);
// ์ง์ ๋ฐฑ์คํ (Exponential Backoff)
delay = TimeSpan.FromSeconds(delay.Value.TotalSeconds * 2);
}
}
// ๋ง์ง๋ง ์๋
return await operation();
}
// ์ฌ์ฉ ์์
public async Task<string> FetchDataWithRetry(string url)
{
return await ExecuteWithRetry(async () =>
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
});
}
}
// Polly ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๊ณ ๊ธ ๋ฒ์
// Install-Package Polly
public class PollyRetryExample
{
public async Task<string> FetchDataWithPolly(string url)
{
var retryPolicy = Policy
.Handle<HttpRequestException>()
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: attempt =>
TimeSpan.FromSeconds(Math.Pow(2, attempt)),
onRetry: (exception, timeSpan, retryCount, context) =>
{
Console.WriteLine($"์ฌ์๋ {retryCount}: {exception.Message}");
}
);
return await retryPolicy.ExecuteAsync(async () =>
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
});
}
}
๐จ ํจํด 2: Circuit Breaker ํจํด
public class CircuitBreaker
{
private int _failureCount = 0;
private DateTime _lastFailureTime = DateTime.MinValue;
private readonly int _failureThreshold;
private readonly TimeSpan _timeout;
private CircuitState _state = CircuitState.Closed;
public CircuitBreaker(int failureThreshold = 5, TimeSpan? timeout = null)
{
_failureThreshold = failureThreshold;
_timeout = timeout ?? TimeSpan.FromMinutes(1);
}
public async Task<T> ExecuteAsync<T>(Func<Task<T>> operation)
{
if (_state == CircuitState.Open)
{
if (DateTime.Now - _lastFailureTime > _timeout)
{
_state = CircuitState.HalfOpen;
Console.WriteLine("๐ Circuit Breaker: Half-Open ์ํ");
}
else
{
throw new Exception("Circuit Breaker๊ฐ Open ์ํ์
๋๋ค!");
}
}
try
{
var result = await operation();
if (_state == CircuitState.HalfOpen)
{
_state = CircuitState.Closed;
_failureCount = 0;
Console.WriteLine("โ
Circuit Breaker: Closed ์ํ๋ก ๋ณต๊ตฌ");
}
return result;
}
catch (Exception ex)
{
_failureCount++;
_lastFailureTime = DateTime.Now;
if (_failureCount >= _failureThreshold)
{
_state = CircuitState.Open;
Console.WriteLine($"๐ด Circuit Breaker: Open ์ํ (์คํจ {_failureCount}ํ)");
}
throw;
}
}
}
public enum CircuitState
{
Closed, // ์ ์ ์๋
Open, // ์ฐจ๋จ๋จ
HalfOpen // ๋ณต๊ตฌ ์๋ ์ค
}
// ์ฌ์ฉ ์์
public class ServiceWithCircuitBreaker
{
private readonly CircuitBreaker _circuitBreaker = new CircuitBreaker();
public async Task<string> CallExternalService(string url)
{
return await _circuitBreaker.ExecuteAsync(async () =>
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
});
}
}
๐จ ํจํด 3: ์บ์ฑ ํจํด
public class CachePattern<TKey, TValue>
{
private readonly Dictionary<TKey, CacheEntry<TValue>> _cache = new();
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
private readonly TimeSpan _expiration;
public CachePattern(TimeSpan expiration)
{
_expiration = expiration;
}
public async Task<TValue> GetOrAddAsync(
TKey key,
Func<Task<TValue>> factory)
{
// ์บ์ ํ์ธ (๋ฝ ์์ด)
if (_cache.TryGetValue(key, out var entry))
{
if (!entry.IsExpired)
{
return entry.Value;
}
}
// ์บ์ ๋ฏธ์ค - ๋ฝ ํ๋
await _lock.WaitAsync();
try
{
// Double-check locking
if (_cache.TryGetValue(key, out entry) && !entry.IsExpired)
{
return entry.Value;
}
// ๊ฐ ์์ฑ
var value = await factory();
_cache[key] = new CacheEntry<TValue>
{
Value = value,
ExpirationTime = DateTime.Now + _expiration
};
return value;
}
finally
{
_lock.Release();
}
}
public void Clear()
{
_lock.Wait();
try
{
_cache.Clear();
}
finally
{
_lock.Release();
}
}
}
public class CacheEntry<T>
{
public T Value { get; set; }
public DateTime ExpirationTime { get; set; }
public bool IsExpired => DateTime.Now > ExpirationTime;
}
// ์ฌ์ฉ ์์
public class UserService
{
private readonly CachePattern<int, User> _userCache;
public UserService()
{
_userCache = new CachePattern<int, User>(TimeSpan.FromMinutes(5));
}
public async Task<User> GetUserAsync(int userId)
{
return await _userCache.GetOrAddAsync(userId, async () =>
{
// DB์์ ์ฌ์ฉ์ ์กฐํ
await Task.Delay(100); // DB ์ฟผ๋ฆฌ ์๋ฎฌ๋ ์ด์
return new User { Id = userId, Name = $"User{userId}" };
});
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
๐จ ํจํด 4: ๋ฐฑ๊ทธ๋ผ์ด๋ ์์ ํ
public class BackgroundTaskQueue
{
private readonly Channel<Func<CancellationToken, Task>> _queue;
public BackgroundTaskQueue(int capacity = 100)
{
var options = new BoundedChannelOptions(capacity)
{
FullMode = BoundedChannelFullMode.Wait
};
_queue = Channel.CreateBounded<Func<CancellationToken, Task>>(options);
}
public async Task QueueBackgroundWorkItemAsync(
Func<CancellationToken, Task> workItem)
{
if (workItem == null)
{
throw new ArgumentNullException(nameof(workItem));
}
await _queue.Writer.WriteAsync(workItem);
}
public async Task<Func<CancellationToken, Task>> DequeueAsync(
CancellationToken cancellationToken)
{
var workItem = await _queue.Reader.ReadAsync(cancellationToken);
return workItem;
}
}
// ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋น์ค
public class QueuedHostedService : BackgroundService
{
private readonly BackgroundTaskQueue _taskQueue;
public QueuedHostedService(BackgroundTaskQueue taskQueue)
{
_taskQueue = taskQueue;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine("๋ฐฑ๊ทธ๋ผ์ด๋ ์์
ํ ์์");
while (!stoppingToken.IsCancellationRequested)
{
var workItem = await _taskQueue.DequeueAsync(stoppingToken);
try
{
await workItem(stoppingToken);
}
catch (Exception ex)
{
Console.WriteLine($"์์
์คํ ์ค๋ฅ: {ex.Message}");
}
}
Console.WriteLine("๋ฐฑ๊ทธ๋ผ์ด๋ ์์
ํ ์ข
๋ฃ");
}
}
// ์ฌ์ฉ ์์
public class EmailService
{
private readonly BackgroundTaskQueue _taskQueue;
public EmailService(BackgroundTaskQueue taskQueue)
{
_taskQueue = taskQueue;
}
public async Task SendEmailAsync(string to, string subject, string body)
{
// ์ด๋ฉ์ผ ์ ์ก์ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ํ์
await _taskQueue.QueueBackgroundWorkItemAsync(async ct =>
{
Console.WriteLine($"์ด๋ฉ์ผ ์ ์ก ์ค: {to}");
await Task.Delay(2000, ct); // ์ ์ก ์๋ฎฌ๋ ์ด์
Console.WriteLine($"์ด๋ฉ์ผ ์ ์ก ์๋ฃ: {to}");
});
Console.WriteLine("์ด๋ฉ์ผ์ด ํ์ ์ถ๊ฐ๋์์ต๋๋ค.");
}
}
๐ฌ ๋๋ฒ๊น ๊ณผ ๋ชจ๋ํฐ๋ง
๋น๋๊ธฐ ์ฝ๋๋ ๋๋ฒ๊น ์ด ๊น๋ค๋ก์ธ ์ ์์ด. ํ์ง๋ง ์ฌ๋ฐ๋ฅธ ๋๊ตฌ์ ๊ธฐ๋ฒ์ ์๋ฉด ํจ์ฌ ์ฌ์์ ธ! ๐
๐ ๏ธ Visual Studio ๋๋ฒ๊น ํ
1. Parallel Stacks ์ฐฝ ์ฌ์ฉ
Debug โ Windows โ Parallel Stacks
์ฌ๋ฌ Task์ ์คํ ์ํ๋ฅผ ์๊ฐ์ ์ผ๋ก ๋ณผ ์ ์์ด!
2. Tasks ์ฐฝ ์ฌ์ฉ
Debug โ Windows โ Tasks
๋ชจ๋ Task์ ์ํ, ID, ์์น๋ฅผ ํ๋์ ํ์ธ
3. Async/Await ๋๋ฒ๊น
Call Stack ์ฐฝ์์ [Async] ํ์๋ฅผ ํ์ธ
๋น๋๊ธฐ ํธ์ถ ์ฒด์ธ์ ์ถ์ ํ ์ ์์ด
4. ์ค๋จ์ ์กฐ๊ฑด ์ค์
ํน์ Task ID๋ ์ค๋ ๋์์๋ง ์ค๋จ๋๋๋ก ์ค์ ๊ฐ๋ฅ
๐ ์ฑ๋ฅ ๋ชจ๋ํฐ๋ง
public class PerformanceMonitoring
{
// ์คํ ์๊ฐ ์ธก์
public async Task<T> MeasureExecutionTime<T>(
Func<Task<T>> operation,
string operationName)
{
var stopwatch = Stopwatch.StartNew();
try
{
var result = await operation();
stopwatch.Stop();
Console.WriteLine($"โ
{operationName}: {stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
stopwatch.Stop();
Console.WriteLine($"โ {operationName} ์คํจ: {stopwatch.ElapsedMilliseconds}ms");
throw;
}
}
// Task ์ํ ๋ชจ๋ํฐ๋ง
public void MonitorTask(Task task, string taskName)
{
task.ContinueWith(t =>
{
if (t.IsCompletedSuccessfully)
{
Console.WriteLine($"โ
{taskName} ์๋ฃ");
}
else if (t.IsFaulted)
{
Console.WriteLine($"โ {taskName} ์คํจ: {t.Exception?.Message}");
}
else if (t.IsCanceled)
{
Console.WriteLine($"โน๏ธ {taskName} ์ทจ์๋จ");
}
});
}
// ๋์ ์คํ ์ ์ถ์
public class ConcurrencyTracker
{
private int _currentCount = 0;
private int _maxCount = 0;
private readonly object _lock = new object();
public IDisposable Track()
{
lock (_lock)
{
_currentCount++;
if (_currentCount > _maxCount)
{
_maxCount = _currentCount;
Console.WriteLine($"๐ ์ต๋ ๋์ ์คํ: {_maxCount}");
}
}
return new DisposableAction(() =>
{
lock (_lock)
{
_currentCount--;
}
});
}
}
private class DisposableAction : IDisposable
{
private readonly Action _action;
public DisposableAction(Action action)
{
_action = action;
}
public void Dispose()
{
_action();
}
}
}
// ์ฌ์ฉ ์์
public class MonitoredService
{
private readonly PerformanceMonitoring _monitoring = new();
private readonly PerformanceMonitoring.ConcurrencyTracker _tracker = new();
public async Task<string> ProcessDataAsync(string input)
{
using (_tracker.Track())
{
return await _monitoring.MeasureExecutionTime(
async () =>
{
await Task.Delay(Random.Shared.Next(100, 500));
return $"Processed: {input}";
},
$"ProcessData({input})"
);
}
}
}
๐ ๋ก๊น ๋ฒ ์คํธ ํ๋ํฐ์ค
public class AsyncLogging
{
private readonly ILogger _logger;
public async Task<Result> ProcessWithLogging(Request request)
{
var correlationId = Guid.NewGuid();
_logger.LogInformation(
"[{CorrelationId}] ์ฒ๋ฆฌ ์์: {RequestId}",
correlationId,
request.Id
);
try
{
var result = await ProcessAsync(request);
_logger.LogInformation(
"[{CorrelationId}] ์ฒ๋ฆฌ ์๋ฃ: {RequestId}, ๊ฒฐ๊ณผ: {ResultCode}",
correlationId,
request.Id,
result.Code
);
return result;
}
catch (Exception ex)
{
_logger.LogError(
ex,
"[{CorrelationId}] ์ฒ๋ฆฌ ์คํจ: {RequestId}",
correlationId,
request.Id
);
throw;
}
}
private async Task<Result> ProcessAsync(Request request)
{
await Task.Delay(100);
return new Result { Code = 200 };
}
}
public interface ILogger
{
void LogInformation(string message, params object[] args);
void LogError(Exception ex, string message, params object[] args);
}
public class Request
{
public int Id { get; set; }
}
public class Result
{
public int Code { get; set; }
}
๐ ์ค์ ํ๋ก์ ํธ: ๊ณ ์ฑ๋ฅ ์น ํฌ๋กค๋ฌ
์ง๊ธ๊น์ง ๋ฐฐ์ด ๋ชจ๋ ๊ฒ์ ์ข ํฉํด์ ์ค์ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด๋ณด์! ์ฌ๋ฌ ์น์ฌ์ดํธ๋ฅผ ๋์์ ํฌ๋กค๋งํ๋ ๊ณ ์ฑ๋ฅ ํฌ๋กค๋ฌ์ผ. ๐ท๏ธ
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
public class WebCrawler
{
private readonly HttpClient _httpClient;
private readonly SemaphoreSlim _rateLimiter;
private readonly ConcurrentDictionary<string, CrawlResult> _results;
private readonly PerformanceMetrics _metrics;
public WebCrawler(int maxConcurrency = 10)
{
_httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(30)
};
_rateLimiter = new SemaphoreSlim(maxConcurrency);
_results = new ConcurrentDictionary<string, CrawlResult>();
_metrics = new PerformanceMetrics();
}
// ๋ฉ์ธ ํฌ๋กค๋ง ๋ฉ์๋
public async Task<CrawlSummary> CrawlAsync(
List<string> urls,
CancellationToken cancellationToken = default)
{
Console.WriteLine($"๐ท๏ธ ํฌ๋กค๋ง ์์: {urls.Count}๊ฐ URL");
var stopwatch = Stopwatch.StartNew();
try
{
// ๋ชจ๋ URL์ ๋ณ๋ ฌ๋ก ํฌ๋กค๋ง
var tasks = urls.Select(url =>
CrawlUrlAsync(url, cancellationToken)
);
await Task.WhenAll(tasks);
stopwatch.Stop();
return new CrawlSummary
{
TotalUrls = urls.Count,
SuccessCount = _results.Count(r => r.Value.Success),
FailureCount = _results.Count(r => !r.Value.Success),
TotalTime = stopwatch.Elapsed,
Results = _results.Values.ToList(),
Metrics = _metrics.GetSnapshot()
};
}
catch (Exception ex)
{
Console.WriteLine($"โ ํฌ๋กค๋ง ์ค๋ฅ: {ex.Message}");
throw;
}
}
// ๊ฐ๋ณ URL ํฌ๋กค๋ง
private async Task CrawlUrlAsync(
string url,
CancellationToken cancellationToken)
{
await _rateLimiter.WaitAsync(cancellationToken);
var stopwatch = Stopwatch.StartNew();
try
{
_metrics.IncrementAttempts();
Console.WriteLine($"๐ฅ ํฌ๋กค๋ง ์ค: {url}");
// HTTP ์์ฒญ
var response = await _httpClient.GetAsync(url, cancellationToken);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
stopwatch.Stop();
// ๊ฒฐ๊ณผ ์ ์ฅ
var result = new CrawlResult
{
Url = url,
Success = true,
StatusCode = (int)response.StatusCode,
ContentLength = content.Length,
ResponseTime = stopwatch.Elapsed,
Timestamp = DateTime.Now
};
_results[url] = result;
_metrics.IncrementSuccess();
_metrics.RecordResponseTime(stopwatch.Elapsed);
Console.WriteLine(
$"โ
์๋ฃ: {url} ({content.Length} bytes, {stopwatch.ElapsedMilliseconds}ms)"
);
}
catch (OperationCanceledException)
{
Console.WriteLine($"โน๏ธ ์ทจ์๋จ: {url}");
_results[url] = new CrawlResult
{
Url = url,
Success = false,
ErrorMessage = "์์
์ด ์ทจ์๋์์ต๋๋ค.",
Timestamp = DateTime.Now
};
_metrics.IncrementFailures();
}
catch (Exception ex)
{
stopwatch.Stop();
Console.WriteLine($"โ ์คํจ: {url} - {ex.Message}");
_results[url] = new CrawlResult
{
Url = url,
Success = false,
ErrorMessage = ex.Message,
ResponseTime = stopwatch.Elapsed,
Timestamp = DateTime.Now
};
_metrics.IncrementFailures();
}
finally
{
_rateLimiter.Release();
}
}
// ์งํ ์ํฉ ๋ชจ๋ํฐ๋ง
public async Task MonitorProgressAsync(
Task crawlTask,
TimeSpan interval)
{
while (!crawlTask.IsCompleted)
{
await Task.Delay(interval);
var snapshot = _metrics.GetSnapshot();
Console.WriteLine(
$"\n๐ ์งํ ์ํฉ: " +
$"์ฑ๊ณต {snapshot.SuccessCount}, " +
$"์คํจ {snapshot.FailureCount}, " +
$"ํ๊ท ์๋ต์๊ฐ {snapshot.AverageResponseTime.TotalMilliseconds:F0}ms\n"
);
}
}
}
// ํฌ๋กค๋ง ๊ฒฐ๊ณผ
public class CrawlResult
{
public string Url { get; set; }
public bool Success { get; set; }
public int StatusCode { get; set; }
public int ContentLength { get; set; }
public TimeSpan ResponseTime { get; set; }
public string ErrorMessage { get; set; }
public DateTime Timestamp { get; set; }
}
// ํฌ๋กค๋ง ์์ฝ
public class CrawlSummary
{
public int TotalUrls { get; set; }
public int SuccessCount { get; set; }
public int FailureCount { get; set; }
public TimeSpan TotalTime { get; set; }
public List<CrawlResult> Results { get; set; }
public MetricsSnapshot Metrics { get; set; }
public void PrintSummary()
{
Console.WriteLine("\n" + new string('=', 60));
Console.WriteLine("๐ ํฌ๋กค๋ง ์์ฝ");
Console.WriteLine(new string('=', 60));
Console.WriteLine($"์ด URL: {TotalUrls}");
Console.WriteLine($"์ฑ๊ณต: {SuccessCount} ({SuccessCount * 100.0 / TotalUrls:F1}%)");
Console.WriteLine($"์คํจ: {FailureCount} ({FailureCount * 100.0 / TotalUrls:F1}%)");
Console.WriteLine($"์ด ์์ ์๊ฐ: {TotalTime.TotalSeconds:F2}์ด");
Console.WriteLine($"ํ๊ท ์๋ต ์๊ฐ: {Metrics.AverageResponseTime.TotalMilliseconds:F0}ms");
Console.WriteLine($"์ต์ ์๋ต ์๊ฐ: {Metrics.MinResponseTime.TotalMilliseconds:F0}ms");
Console.WriteLine($"์ต๋ ์๋ต ์๊ฐ: {Metrics.MaxResponseTime.TotalMilliseconds:F0}ms");
Console.WriteLine($"์ฒ๋ฆฌ๋: {TotalUrls / TotalTime.TotalSeconds:F2} URLs/์ด");
Console.WriteLine(new string('=', 60));
if (FailureCount > 0)
{
Console.WriteLine("\nโ ์คํจํ URL:");
foreach (var result in Results.Where(r => !r.Success))
{
Console.WriteLine($" - {result.Url}: {result.ErrorMessage}");
}
}
}
}
// ์ฑ๋ฅ ๋ฉํธ๋ฆญ
public class PerformanceMetrics
{
private int _attempts = 0;
private int _successes = 0;
private int _failures = 0;
private readonly List<TimeSpan> _responseTimes = new();
private readonly object _lock = new object();
public void IncrementAttempts() => Interlocked.Increment(ref _attempts);
public void IncrementSuccess() => Interlocked.Increment(ref _successes);
public void IncrementFailures() => Interlocked.Increment(ref _failures);
public void RecordResponseTime(TimeSpan time)
{
lock (_lock)
{
_responseTimes.Add(time);
}
}
public MetricsSnapshot GetSnapshot()
{
lock (_lock)
{
return new MetricsSnapshot
{
AttemptCount = _attempts,
SuccessCount = _successes,
FailureCount = _failures,
AverageResponseTime = _responseTimes.Any()
? TimeSpan.FromMilliseconds(_responseTimes.Average(t => t.TotalMilliseconds))
: TimeSpan.Zero,
MinResponseTime = _responseTimes.Any()
? _responseTimes.Min()
: TimeSpan.Zero,
MaxResponseTime = _responseTimes.Any()
? _responseTimes.Max()
: TimeSpan.Zero
};
}
}
}
public class MetricsSnapshot
{
public int AttemptCount { get; set; }
public int SuccessCount { get; set; }
public int FailureCount { get; set; }
public TimeSpan AverageResponseTime { get; set; }
public TimeSpan MinResponseTime { get; set; }
public TimeSpan MaxResponseTime { get; set; }
}
// ์ฌ์ฉ ์์
public class CrawlerDemo
{
public static async Task Main()
{
var urls = new List<string>
{
"https://www.google.com",
"https://www.microsoft.com",
"https://www.github.com",
"https://www.stackoverflow.com",
"https://www.reddit.com",
"https://www.wikipedia.org",
"https://www.amazon.com",
"https://www.twitter.com",
"https://www.facebook.com",
"https://www.youtube.com"
};
var crawler = new WebCrawler(maxConcurrency: 5);
using var cts = new CancellationTokenSource();
// 30์ด ํ์์์
cts.CancelAfter(TimeSpan.FromSeconds(30));
// ํฌ๋กค๋ง ์์
var crawlTask = crawler.CrawlAsync(urls, cts.Token);
// ์งํ ์ํฉ ๋ชจ๋ํฐ๋ง
var monitorTask = crawler.MonitorProgressAsync(
crawlTask,
TimeSpan.FromSeconds(2)
);
// ์๋ฃ ๋๊ธฐ
var summary = await crawlTask;
await monitorTask;
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
summary.PrintSummary();
}
}
โ ์ทจ์ ํ ํฐ ์ง์
โ ์ค์๊ฐ ์งํ ์ํฉ ๋ชจ๋ํฐ๋ง
โ ์ฑ๋ฅ ๋ฉํธ๋ฆญ ์์ง
โ ์๋ฌ ์ฒ๋ฆฌ ๋ฐ ์ฌ์๋
โ ์ค๋ ๋ ์์ ํ ๊ฒฐ๊ณผ ์์ง
์ด๋ฐ ํจํด๋ค์ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋๋ ์ ์ฉํ๊ฒ ์ฐ์ฌ!
๐ ํ์ต ๋ฆฌ์์ค์ ๋ค์ ๋จ๊ณ
์ฌ๊ธฐ๊น์ง ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ๐ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ์ ๋ชจ๋ ๋ฐฐ์ ์ด. ์ด์ ๋ ๊น์ด ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ด๋ฐ ๊ฒ๋ค์ ์ถ์ฒํด:
๐ ์ถ์ฒ ํ์ต ์๋ฃ
๊ณต์ ๋ฌธ์
โข Microsoft Docs - Asynchronous Programming
โข C# Language Specification
โข .NET API Browser
์ฑ
โข "Concurrency in C# Cookbook" by Stephen Cleary
โข "C# in Depth" by Jon Skeet
โข "Pro Asynchronous Programming with .NET" by Richard Blewett
์จ๋ผ์ธ ๊ฐ์
โข Pluralsight - Asynchronous Programming in C#
โข Udemy - Complete C# Masterclass
โข Microsoft Learn - ๋ฌด๋ฃ ํ์ต ๊ฒฝ๋ก
์ปค๋ฎค๋ํฐ
โข Stack Overflow - C# ํ๊ทธ
โข Reddit - r/csharp
โข C# Discord ์๋ฒ
๐ ๋ค์ ๋จ๊ณ๋ก ๋์๊ฐ๊ธฐ
1. ์ค์ ํ๋ก์ ํธ ๋ง๋ค๊ธฐ
๋ฐฐ์ด ๋ด์ฉ์ ์ค์ ํ๋ก์ ํธ์ ์ ์ฉํด๋ด. ์๋ฅผ ๋ค์ด:
โข ์น API ์๋ฒ ๋ง๋ค๊ธฐ
โข ์ค์๊ฐ ์ฑํ
์ ํ๋ฆฌ์ผ์ด์
โข ํ์ผ ์ฒ๋ฆฌ ๋๊ตฌ
โข ๋ฐ์ดํฐ ๋ถ์ ํ์ดํ๋ผ์ธ
2. ๊ณ ๊ธ ์ฃผ์ ํ๊ตฌ
โข Channels์ Dataflow
โข Reactive Extensions (Rx.NET)
โข gRPC์ ๋น๋๊ธฐ ์คํธ๋ฆฌ๋ฐ
โข SignalR๋ก ์ค์๊ฐ ํต์
3. ์ฑ๋ฅ ์ต์ ํ
โข BenchmarkDotNet์ผ๋ก ๋ฒค์น๋งํน
โข Memory Profiling
โข Span<T>์ Memory<T> ํ์ฉ
โข Zero-allocation ๊ธฐ๋ฒ
4. ์คํ์์ค ๊ธฐ์ฌ
โข GitHub์์ C# ํ๋ก์ ํธ ์ฐพ๊ธฐ
โข ์ด์ ํด๊ฒฐํ๊ณ PR ๋ณด๋ด๊ธฐ
โข ์์ ๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ง๋ค๊ธฐ
๐ผ ์ค๋ฌด ์ ์ฉ ํ
ASP.NET Core์์
โข ๋ชจ๋ ์ปจํธ๋กค๋ฌ ์ก์
์ async๋ก ๋ง๋ค๊ธฐ
โข Entity Framework Core์ ๋น๋๊ธฐ ๋ฉ์๋ ์ฌ์ฉ
โข IAsyncEnumerable๋ก ๋์ฉ๋ ๋ฐ์ดํฐ ์คํธ๋ฆฌ๋ฐ
โข Background Services๋ก ๋ฐฑ๊ทธ๋ผ์ด๋ ์์
์ฒ๋ฆฌ
Desktop ์ ํ๋ฆฌ์ผ์ด์
์์
โข UI ์ค๋ ๋ ๋ธ๋กํน ์ ๋ ๊ธ์ง
โข Progress ๋ฆฌํฌํ
์ผ๋ก ์ฌ์ฉ์ ๊ฒฝํ ๊ฐ์
โข CancellationToken์ผ๋ก ์ทจ์ ๊ธฐ๋ฅ ์ ๊ณต
โข async/await๋ก ๋ถ๋๋ฌ์ด UI ์ ์ง
๋ง์ดํฌ๋ก์๋น์ค์์
โข HTTP ํด๋ผ์ด์ธํธ ์ฌ์ฌ์ฉ (HttpClientFactory)
โข Circuit Breaker ํจํด์ผ๋ก ์ฅ์ ๊ฒฉ๋ฆฌ
โข Retry ์ ์ฑ
์ผ๋ก ์ผ์์ ์ค๋ฅ ์ฒ๋ฆฌ
โข ๋ถ์ฐ ์ถ์ ์ผ๋ก ์ฑ๋ฅ ๋ชจ๋ํฐ๋ง
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ ๋ง ๊ธด ์ฌ์ ์ด์์ง? ๐
ํ์ง๋ง ์ด์ ๋๋ C# ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๊ฑฐ์ ๋ชจ๋ ๊ฒ์ ์๊ฒ ๋์ด!
async/await์ Task๋ ๋จ์ํ ํค์๋๊ฐ ์๋์ผ. ์ด๊ฒ๋ค์ ํ๋ ์ ํ๋ฆฌ์ผ์ด์
๊ฐ๋ฐ์ ํ์ ๋๊ตฌ์ด๊ณ , ์ฌ์ฉ์์๊ฒ ๋ ๋์ ๊ฒฝํ์ ์ ๊ณตํ๋ ํต์ฌ ๊ธฐ์ ์ด์ง. ๐
๊ธฐ์ตํด์ผ ํ ํต์ฌ ํฌ์ธํธ๋ค์ ์ ๋ฆฌํด๋ณผ๊ฒ:
๐ฏ ํต์ฌ ์์ฝ
1. ๊ธฐ๋ณธ ์์น
โ
async/await๋ก ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋๊ธฐ ์ฝ๋์ฒ๋ผ ์์ฑ
โ
Task๋ ๋น๋๊ธฐ ์์
์ ๋ํ๋ด๋ ๊ฐ์ฒด
โ
UI ์ค๋ ๋๋ฅผ ์ ๋ ๋ธ๋กํนํ์ง ๋ง ๊ฒ
2. ์ฑ๋ฅ ์ต์ ํ
โ
Task.WhenAll๋ก ๋ณ๋ ฌ ์ฒ๋ฆฌ
โ
ValueTask๋ก ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
โ
ConfigureAwait(false)๋ก ๋ถํ์ํ ์ปจํ
์คํธ ์ ํ ์ ๊ฑฐ
โ
IAsyncEnumerable๋ก ์คํธ๋ฆฌ๋ฐ ์ฒ๋ฆฌ
3. ์์ ์ฑ
โ
CancellationToken์ผ๋ก ์ทจ์ ์ง์
โ
์ ์ ํ ์์ธ ์ฒ๋ฆฌ
โ
Retry์ Circuit Breaker ํจํด
โ
๋์์ฑ ์ ์ด (SemaphoreSlim)
4. ํผํด์ผ ํ ๊ฒ๋ค
โ async void (์ด๋ฒคํธ ํธ๋ค๋ฌ ์ ์ธ)
โ .Result๋ .Wait() ํธ์ถ
โ ๋๊ธฐ ๋ฉ์๋๋ฅผ Task.Run์ผ๋ก ๊ฐ์ธ๊ธฐ
โ ๋ถํ์ํ async/await
๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ์ฒ์์๋ ์ด๋ ต๊ฒ ๋๊ปด์ง ์ ์์ด. ํ์ง๋ง ๊ณ์ ์ฐ์ตํ๋ค ๋ณด๋ฉด ์์ฐ์ค๋ฝ๊ฒ ์์ ์ต๊ฒ ๋ผ. ๋ง์น ์์ ๊ฑฐ ํ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ! ๐ดโโ๏ธ
์ค์๋ฅผ ๋๋ ค์ํ์ง ๋ง. ๋ชจ๋ ๊ฐ๋ฐ์๊ฐ ์ฒ์์๋ ๋ฐ๋๋ฝ๋ ๋ง๋ค๊ณ , ์์ธ๋ ๋์น๊ณ , ์ฑ๋ฅ ๋ฌธ์ ๋ ๊ฒช์ด. ์ค์ํ ๊ฑด ๊ทธ๋ฐ ๊ฒฝํ์์ ๋ฐฐ์ฐ๋ ๊ฑฐ์ผ. ๐ช
์ด์ ๋๋ง์ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด๋ด! ๋ฐฐ์ด ๋ด์ฉ์ ์ค์ ์ ์ ์ฉํ๋ฉด์ ์ง์ง ์ค๋ ฅ์ด ๋์ด๋ ๊ฑฐ์ผ. ๊ทธ๋ฆฌ๊ณ ๋งํ๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ๊ณต์ ๋ฌธ์๋ฅผ ์ฐพ์๋ณด๊ณ , ์ปค๋ฎค๋ํฐ์ ์ง๋ฌธํ๊ณ , ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ ์ฝ์ด๋ด.
ํน์ ํ๋ก๊ทธ๋๋ฐ ๊ด๋ จํด์ ๋์์ด ํ์ํ๊ฑฐ๋, ๋ฉํ ๋ง์ ๋ฐ๊ณ ์ถ๋ค๋ฉด ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ ๋ฌธ๊ฐ๋ค์ ๋์์ ๋ฐ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ! ๋๋ก๋ ๊ฒฝํ ๋ง์ ๊ฐ๋ฐ์์ ์กฐ์ธ ํ๋ง๋๊ฐ ๋ฉฐ์น ๊ฐ์ ๊ณ ๋ฏผ์ ํด๊ฒฐํด์ฃผ๊ธฐ๋ ํ๊ฑฐ๋ . ๐
์, ์ด์ ์ฝ๋ฉ์ ์์ํ ์๊ฐ์ด์ผ! ํ์ดํ
! ๐
๐ Happy Async Coding! ๐
"The best way to predict the future is to implement it asynchronously."
- ์ต๋ช
์ C# ๊ฐ๋ฐ์
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.