Server/κ²Œμž„ μ„œλ²„(λ„€νŠΈμ›Œν¬, λ©€ν‹°μ“°λ ˆλ“œ,OS)

μ“°λ ˆλ“œ 생성, μ“°λ ˆλ“œν’€λ§, Task λͺ…μ‹œ

Rainbow🌈Coder 2022. 4. 15. 21:40
728x90
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 인자λ₯Ό λžŒλ‹€λ¬Έ λ‹€μŒμœΌλ‘œ μ•ˆ λ„£μ—ˆμœΌλ©΄ μ—­μ‹œ μž‘λ™μ΄ μ•ˆλœλ‹€.

728x90