C# :: Code & APIs

[C#] 초간단 Threading (쓰레딩) 구현 코드

[C#] 초간단 Threading (쓰레딩) 구현 코드

using System;
using System.Threading;

namespace ThreadingCounter
{
    class Program
    {
        static void Main(string[] args)
        {
            Counter counter = new Counter();
            counter.Run();
        }
    }

    class Counter
    {
        public void Incrementer()
        {
            for (int i = 1; i <= 300; i++)
            {
                Console.WriteLine("증가: {0}", i);
                Thread.Sleep(100);
            }
        }

        public void Decrementer()
        {
            for (int i = 300; i >= 1; i--)
            {
                Console.WriteLine("\t감소: {0}", i);
                Thread.Sleep(50);
            }
        }

        public void Run()
        {
            // ThreadStart Delegate (델리게이트) 역할을 합니다.
            ThreadStart start1 = this.Incrementer;
            ThreadStart start2 = this.Decrementer;

            Thread thread1 = new Thread(start1);
            Thread thread2 = new Thread(start2);

            // Thread 를 시작하는 코드.
            thread1.Start();
            thread2.Start();
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: