μ€λ λ(Thread)
λ©ν° μ€λ λ
λ©ν° μ€λ λ©μ λ§ κ·Έλλ‘ νλμ νλ‘κ·Έλ¨μμ μ¬λ¬ κ°μ μ€λ λλ₯Ό λμμ μ€ννλ κ²μ μλ―Ένλ€. κ° μ€λ λλ λ 립μ μΌλ‘ μ€νλλ©°, λ³λ ¬ μ²λ¦¬κ° κ°λ₯νκΈ° λλ¬Έμ μ±λ₯μ΄ ν₯μλ μ μλ€. νμ§λ§ λ무 κ³Όν λ©ν° μ€λ λ©μ μ€νλ € μ±λ₯μ μ νμν€κ³ , ꡬνμ΄ λ³΅μ‘νκΈ° λλ¬Έμ μ μ ν μν©μμ μ¬μ©νμ¬μΌ νλ€.
μ€λ λ μμνλ λ²
1. Thread μΈμ€ν΄μ€ μμ±
2. Thread.Start()λ‘ μ€λ λ μμ
3. Thread.Join()μΌλ‘ μ€λ λ μ’
λ£ λκΈ°
// No Parameter
Thread thread = new Thread(Run);
thread.Start();
thread.Join();
// Parameter exist
Thread thread2 = new Thread(() => Run2(2, 3));
thread2.Start();
thread2.Join();
Thread.Start()λ λ§ κ·Έλλ‘ μ€λ λλ₯Ό μμνλ€λ κ²μ΄κΈ° λλ¬Έμ μ μ΄ν΄κ° λμ§λ§, Tread.Join()μ μ΄ν΄νκΈ° μ λ§€ν μλ μλ€. thread.Join()μ threadκ° μμλκ³ μ’ λ£λ λκΉμ§ μμ μ λ©μΆκ³ κΈ°λ€λ¦°λ€λ λ»μ΄λ€.
Joinμ νμ©νκΈ° μν΄ λ¨Όμ λ λ©μλλ₯Ό λ§λ€μλ€.
DoWork1μ 5μ΄λ₯Ό μΈκ³ , DoWork2λ 2μ΄λ₯Ό μΌλ€.
static void DoWork1()
{
for (int i = 1; i <= 5; i++) {
Thread.Sleep(1000);
Console.WriteLine($"thread1: {i} sec");
}
Console.WriteLine("Work 1 completed.");
}
static void DoWork2()
{
for (int i = 1; i <= 2; i++) {
Thread.Sleep(1000);
Console.WriteLine($"thread2: {i} sec");
}
Console.WriteLine("Work 2 completed.");
}
λ λ©μλλ₯Ό μΈμ€ν΄μ€λ‘ κ°μ§λ μ€λ λλ₯Ό μμ±ν΄μ μλμ κ°μ μμλ‘ μ€νμν€λ©΄,
Thread thread1 = new Thread(DoWork1);
Thread thread2 = new Thread(DoWork2);
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine("Both threads have completed their work.");
Console.WriteLine();
thread1μ΄ μ’ λ£λκΈ°λκΈ° μ μ thread2λ start λμμΌλ μλμ κ°μ κ²°κ³Όκ° λμ¨λ€.
thread2: 1 sec
thread1: 1 sec
thread2: 2 sec
Work 2 completed.
thread1: 2 sec
thread1: 3 sec
thread1: 4 sec
thread1: 5 sec
Work 1 completed.
Both threads have completed their work.
λ§μ½ μμλ₯Ό μ΄λ κ² λ°κΏμ thread2κ° μμλκΈ° μ μ thread1μ΄ λ¨Όμ μ’ λ£λλλ‘ Joinμ λ£μΌλ©΄ μλμ κ°μ΄ κ²°κ³Όκ° λ°λλ€.
thread1.Start();
thread1.Join();
thread2.Start();
thread2.Join();
thread1: 1 sec
thread1: 2 sec
thread1: 3 sec
thread1: 4 sec
thread1: 5 sec
Work 1 completed.
thread2: 1 sec
thread2: 2 sec
Work 2 completed.
Both threads have completed their work.
μ€λ λ μ€μ§μν€κΈ°
μ€λ λλ₯Ό μ·¨μμν€λ Abort() λ©μλλ μμ§λ§, κΆμ₯λμ§ μλλ€. λ§μ½ thread Start()κ° κ³μ λ°λ³΅λλ λ°λ³΅λ¬Έ μμ ν¬ν¨λμ΄ μλ€λ©΄, μ€λ λ μ’ λ£ νλκ·Έ λ±μ μ¬μ©ν΄μ μ€λ λλ₯Ό μνν κ²μΈμ§ μν κ²μΈμ§λ₯Ό νλ¨νκ² νκ±°λ, Interruptλ₯Ό μ¬μ©ν΄μ μ€μ§μν€λ κ²μ΄ μ’λ€.
Interrupted λ©μλλ₯Ό μλμν€λ©΄, ThreadInterruptedExceptionμ΄ λ°μνκ³ , μ΄ exceptionμ΄ λ°μνλ©΄ νμ¬ μ€λ λλ₯Ό μ€λ¨μν¨λ€.
μλμ κ°μ΄ try catchλ‘ ThreadInterruptedExceptionμ μ‘μ μ μλ€.
try {
int count = 0;
while (true) {
Thread.Sleep(1000);
count++;
Console.WriteLine($"{count} sec");
if (count == n) {
Thread.CurrentThread.Interrupt();
}
}
}
catch (ThreadInterruptedException) {
Console.WriteLine("Thread was interrupted.");
}
μ ν¨μλ₯Ό DoWork(int n) λ©μλλΌκ³ νκ³ μ€νμν€λ©΄
static void Main()
{
Thread thread = new Thread(() => DoWork(5));
Console.WriteLine($"Thread State: {thread.ThreadState}");
thread.Start();
Console.WriteLine($"Thread State: {thread.ThreadState}");
thread.Join();
Console.WriteLine($"Thread State: {thread.ThreadState}");
}
Thread State: Unstarted
Thread State: Running
Thread is starting.
1 sec
2 sec
3 sec
4 sec
5 sec
Thread was interrupted.
Thread State: Stopped
μ΄λ κ² μ€νλλ€.
μ€λ λ μμμ μ λλ‘ ν΄μ νμ§ μμΌλ©΄ λ©λͺ¨λ¦¬μ μμμ΄ λμλκΈ° λλ¬Έμ μ±λ₯μ μ νμν€κ² λλ€. Joinκ³Ό Interrupt, κ·Έλ¦¬κ³ ν΄μ νκΈ° μν flag λ±μ λ°©λ²μ μ¬μ©ν΄μ μ€λ λλ₯Ό μ λ ν΄μ νμ¬μΌ νλ€.
'π₯οΈ > C#' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C#] LINQμ Single()μ λν΄μ (0) | 2023.10.12 |
---|---|
[C#] μ€λ λμ ν¬λ¦¬ν°μ»¬ μΉμ (Critical Section) (0) | 2023.10.11 |
[C#] 리νλ μ (Reflection): λμ λ©μλ μμ±νκΈ° (3) | 2023.10.11 |
[C#] 리νλ μ (Reflection): λμ μΈμ€ν΄μ€ μμ±νκΈ° (0) | 2023.10.10 |
[C#] 리νλ μ (Reflection) (0) | 2023.10.10 |