μ°λ λ μμ±, μ°λ λνλ§, Task λͺ μ
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread()
{
Console.WriteLine("hello Thread!");
}
static void Main(string[] args)
{
Thread t = new Thread(MainThread);
t.Start();
//λ³λμ μ°λ λκ° μ€νλ¨
Console.WriteLine("Hello World!");
}
}
}
λ°±κ·ΈλΌμ΄λ μ°λ λ
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread()
{
while(true)
Console.WriteLine("hello Thread!");
}
static void Main(string[] args)
{
Thread t = new Thread(MainThread);
//λ°±κ·ΈλΌμ΄λ μ°λ λλ‘, μκ·ΈλΌμ΄λμμ λλ¦΄μ§ μ νν μ μμ
t.IsBackground = true;
t.Start();
//λ³λμ μ°λ λκ° μ€νλ¨
Console.WriteLine("Hello World!");
}
}
}
μ΄λ κ² νλ©΄ μ€νμ κ³μλκ³ μ’ λ£λμ§ μλ μν©μ΄ νΌμ³μ§λ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
while(true)
Console.WriteLine("hello Thread!");
}
static void Main(string[] args)
{
Thread t = new Thread(MainThread);
t.Name = "TestThread";
//λ°±κ·ΈλΌμ΄λ μ°λ λλ‘, μκ·ΈλΌμ΄λμμ λλ¦΄μ§ μ νν μ μμ
t.IsBackground = true;
t.Start();
Console.WriteLine("waiting for thread!");
t.Join(); // λ°±κ·ΈλΌμ΄λκ° λλ λ κΉμ§ κΈ°λ€λ Έλ€κ° λ€μμ€λ‘ κ°κ² λ€
//λ³λμ μ°λ λκ° μ€νλ¨
Console.WriteLine("Hello World!");
}
}
}
μΌμΌν μ°λ λλ₯Ό λ§λ€μ΄μ κ΄λ¦¬νκΈ°λ λ Έκ°λ€μ€λ½λ€.
κ·Έλμ μ¬μ©νκ² λλ μ°λ λν κ°λ
<μ°λ λνλ§>=μΈλ ₯μ¬λ¬΄μ
μ°λ λνλ‘ λκΈ΄ μ°λ λλ μλμΌλ‘ λ°±κ·ΈλΌμ΄λ μ°λ λκ° λλ€.
new Thread()λ‘ λ§λλ λ μμ μ μ§μ κ°λ μ΄κ³ (κ³μ κ΄λ¦¬ νμ)
μ°λ λνλ‘ λκΈ°λ μ°λ λλ λ¨κΈ°λ‘ κ³ μ©νλ λκΈ°μ‘° μλ° κ°λ μ΄λΌκ³ μκ°νλ©΄ μ½λ€.
μλ°κ° λ겨 λ°μ ν μΌμ λ€ μννκ³ λλλ©΄ λ€μ λκΈ°νλ‘ λμκ°μ μΌ λ°κΈ°λ₯Ό κΈ°λ€λ¦¬κ² λλ€-
new λ‘ μΌμΌν λ§λ€κΈ° λ³΄λ€ μ λμ μΌλ‘ μ€λΉκ° λ λ μλ€λΆν° μΌμ μν¬ μ μμΌλκΉ νΈλ¦¬νλ€
λν μ°λ λνμ μ΅λλ‘ λ릴 μ μμ λ§νΌμ μ°λ λκ° μ νμ΄ λ μνμμ λκΈ°μ€μ΄λ€.
무μμ μ°λ λ μλ‘ μμ±(κ³ μ©)νλ κ²μ΄ μλλΌ κΈ°μ‘΄μ λμ Έμ§ μ°λ λλ€μ΄ λμμμ λ€μ μΌκ°μ μ²λ¦¬νλ¬ κ°λ€.
κ·Έλ°λ° νΉμ μΌλ€μ΄ λ무 κ³Όνλ©΄ κΈ°μ‘΄μ λμ Έμ§ μ°λ λλ€μ΄ λμμ€μ§ μλ λ¬Έμ κ° μκΈ°λ νλ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
while(true)
Console.WriteLine("hello Thread!");
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(MainThread);
}
}
}
κ΄λ ¨ μμ
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("hello Thread!");
}
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(MainThread);
while (true)
{
}
}
}
}
μ¬μ€ new threadλ 1000κ°κΉμ§ λμ μμ±, λ릴 μ μλ€.(κ·Έλ¬λ μμ±ν λͺ¨λ μ°λ λκ° μΌνμ§ μμ. λΆνλ μκ°νλ©΄, νμ€μ μ΄μ§ μλ€)
cpu μ½μ΄μλ λ§μΆ°μ£Όλκ² best
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("hello Thread!");
}
}
static void Main(string[] args)
{
//μ°λ λλ₯Ό 무μμ μμ±ν΄λ΄λ κ²μ μ΅μ
μ κ²½μ°μ΄λ€.
for (int i = 0; i <1000; i++)
{
Thread t = new Thread(MainThread);
t.Name = "TestThread";
//λ°±κ·ΈλΌμ΄λ μ°λ λλ‘, μκ·ΈλΌμ΄λμμ λλ¦΄μ§ μ νν μ μμ
t.IsBackground = true;
t.Start();
}
Console.WriteLine("waiting for thread!");
//t.Join(); // λ°±κ·ΈλΌμ΄λκ° λλ λ κΉμ§ κΈ°λ€λ Έλ€κ° λ€μμ€λ‘ κ°κ² λ€
//λ³λμ μ°λ λκ° μ€νλ¨
Console.WriteLine("Hello World!");
while (true)
{
}
}
}
}
μ°λ λ μ΅μ, μ΅λ μ€μ ν΄λ³΄κΈ°
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("hello Thread!");
}
}
static void Main(string[] args)
{
//μ΅μλ 1κ°μ΄κ³ μ΅λλ 5κ°μ΄λ€λΌκ³ μ€μ ν΄μ€μ
////μ¦ 5κ°κ° λ€ λκ³ μμΌλ©΄ λ€λ₯Έ μΌκ°μ΄ λ€μ΄μλ
///λμ΄μ μ§νν μ μλ€λ λ»
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(5, 5);
for (int i = 0; i < 5; i++)
{
ThreadPool.QueueUserWorkItem((obj)=> { while (true) { } } );
}
ThreadPool.QueueUserWorkItem(MainThread);
Console.WriteLine("Hello World!");
while (true)
{
}
}
}
}
Console.WriteLine("hello Thread!"); μ€νμ΄ μλλ μ΄μ λ Main ν¨μ μμ FORλ¬Έμ΄ μ°λ λλ₯Ό λ€ λΉΌκ°κΈ° λλ¬Έμ΄λ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("hello Thread!");
}
}
static void Main(string[] args)
{
//μ΅μλ 1κ°μ΄κ³ μ΅λλ 5κ°μ΄λ€λΌκ³ μ€μ ν΄μ€μ
////μ¦ 5κ°κ° λ€ λκ³ μμΌλ©΄ λ€λ₯Έ μΌκ°μ΄ λ€μ΄μλ
///λμ΄μ μ§νν μ μλ€λ λ»
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(5, 5);
for (int i = 0; i < 4; i++)
{
ThreadPool.QueueUserWorkItem((obj)=> { while (true) { } } );
}
ThreadPool.QueueUserWorkItem(MainThread);
Console.WriteLine("Hello World!");
while (true)
{
}
}
}
}
forλ¬Έμ 4λ²λ§ λ리면 Console.WriteLine("hello Thread!"); μ€νμ΄ μλλ λͺ¨μ΅
μ°λ λνμ μΌκΎΌμ΄ 1κ° λ¨μ μνμκ³ κ·Έ μΌκΎΌμ΄ ν¬λ‘μ°λ λλ₯Ό μΌνλ¬ κ°κΈ°λλ¬Έμ΄λ€.
μ΄λ° μμΌλ‘ μ°λ λνμ΄ μ’κΈ°λ νλ° νΉμ μμ λ€μ΄ μΌκΎΌλ€μ λ€ μ‘μλκ³ μ λμ£Όλ©΄ μμ€ν μ΄ λ¨Ήν΅μ΄ λ μ μλ€λ μ μ΄ μν μμκ° λλ€.
μ΄λ₯Ό 극볡νλ λ°©λ²μ΄ Taskμ΄λ€.
Task t; λ
μ§μμ κ³ μ©νλ€λ λλ보λ€λ μ§μμ΄ ν μΌκ° λ¨μλ₯Ό νλ‘κ·Έλλ¨Έκ° μ μνκ² λ€λ κ°λ μ΄λ€.
using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ServerCore
{
class Program
{
static void MainThread(object state)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("hello Thread!");
}
}
static void Main(string[] args)
{
//μ΅μλ 1κ°μ΄κ³ μ΅λλ 5κ°μ΄λ€λΌκ³ μ€μ ν΄μ€μ
////μ¦ 5κ°κ° λ€ λκ³ μμΌλ©΄ λ€λ₯Έ μΌκ°μ΄ λ€μ΄μλ
///λμ΄μ μ§νν μ μλ€λ λ»
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(5, 5);
for (int i = 0; i < 5; i++)
{
//μ΄ μμ
μ νΉμ΄νλ―λ‘ λ§€μ° μ€λ걸릴 κ²μ΄λ€.
//μ¦ κΈ°μ‘΄ μ€μ νλ μ°λ λνμμ λ½μ μ°μ§ μκ³
//λ³λ μ²λ¦¬κ° λ€μ΄κ° κ²μ΄λ€.
Task t = new Task(() => { while (true) { } }, TaskCreationOptions.LongRunning);
t.Start();
}
ThreadPool.QueueUserWorkItem(MainThread);
Console.WriteLine("Hello World!");
while (true)
{
}
}
}
}
λλκ²λ Console.WriteLine("hello Thread!"); λ°λ‘ λ°λ³΅λ¬Έ μ€νμ΄ λλ κ²μ λ³Ό μ μλ€ ^^
λ¬Όλ‘ TaskCreationOptions.LongRunning μΈμλ₯Ό λλ€λ¬Έ λ€μμΌλ‘ μ λ£μμΌλ©΄ μμ μλμ΄ μλλ€.