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 ์ธ์๋ฅผ ๋๋ค๋ฌธ ๋ค์์ผ๋ก ์ ๋ฃ์์ผ๋ฉด ์ญ์ ์๋์ด ์๋๋ค.
'Server > ๊ฒ์ ์๋ฒ(๋คํธ์ํฌ, ๋ฉํฐ์ฐ๋ ๋,OS)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฒ๋?(๊ฒ์์๋ฒ์ ์น์๋ฒ์ ์ฐจ์ด์ ) (0) | 2022.03.03 |
---|