基本

  • Thread基本
class Program
{
    static void Main(string[] args)
    {
        Thread t1 = new Thread(new ThreadStart(MethodA));
        t1.Start();
        t1.Join();

        MethodB();

        Console.WriteLine("...end");
        Console.ReadLine();
    }

    static void MethodA() 
    {
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine("A"+i.ToString());
        }
    }

    static void MethodB()
    {
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine("B"+i.ToString());
        }
    }
}
  • Threadパラメータ
    class Program
    {
        static void Main(string[] args)
        {
            ThreadClass tc = new ThreadClass(10, "TEST");
            Thread t = new Thread(new ThreadStart(tc.Method));
            t.Start();

            Console.WriteLine("...end");
            Console.ReadLine();
        }

        class ThreadClass 
        {
            int loopCount;
            string outData;

            private ThreadClass(){}
            
            public ThreadClass(int count, string str) 
            {
                this.loopCount = count;
                this.outData = str;
            }

            public void Method() 
            {
                for (int i = 0; i < this.loopCount; i++)
                {
                    Console.WriteLine(this.outData + i.ToString());
                }
            }
        }
    }
  • スレットプール
class Program
{
    static void Main(string[] args)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(MethodC), "AA");
        ThreadPool.QueueUserWorkItem(new WaitCallback(MethodC), "BB");

        Console.WriteLine("...end");
        Console.ReadLine();
    }

    static void MethodC(object obj)
    {
        string data = obj as string; 
		
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine(data + i.ToString());
        }
    }
  • デリゲートによるマルチスレッド
class Program
{
    private static Func<string, DateTime> callback;

    static void Main(string[] args)
    {
        callback= MethodD;
        callback.BeginInvoke("A", new AsyncCallback(MyCallback), DateTime.Now);

        Console.WriteLine("...end");
        Console.ReadLine();
    }

    private static DateTime MethodD(string data)
    {
        for (int i = 0; i < 100; i++)
        {
            Console.Write(data);
        }
        
        return DateTime.Now;
    }

    private static void MyCallback(IAsyncResult ar) 
    {
        DateTime result = callback.EndInvoke(ar); 
        DateTime beginTime = (DateTime)ar.AsyncState; 

        Console.WriteLine();
        Console.WriteLine("{0}に処理を開始し、{1}に処理を完了しました。",beginTime, result);
    }
}
  • タイマーを使ったスレッドの実行
class Program
{
    static void Main(string[] args)
    {
        TimerCallback tc = new TimerCallback(MethodE);
        Console.WriteLine(DateTime.Now.ToString());
        Timer timer = new Timer(tc, null, 0, 1000);
        
        Console.WriteLine("...end");
        Console.ReadLine();
    }

    private static void MethodE(object state)
    {
        Console.WriteLine(DateTime.Now.ToString());
    }
}
  • 排他(lock)
    class Program
    {
        private static int count = 0;
        private static object lockObj = new object();
        
        static void Main(string[] args)
        {
            Thread t1 = new Thread(new ThreadStart(MethodF));
            t1.Start();
            Thread t2 = new Thread(new ThreadStart(MethodG));
            t2.Start();
            
            Console.WriteLine("...end");
            Console.ReadLine();
        }

        static void MethodF()
        {
            lock (lockObj)
            {
                for (int i = 0; i < 100; i++)
                {
                    count++;
                    Console.WriteLine("(+)count=" + count.ToString());
                }
            }
        }

        static void MethodG()
        {
            lock (lockObj)
            {
                for (int i = 0; i < 100; i++)
                {
                    count--;
                    Console.WriteLine("(-)count=" + count.ToString());
                }
            }
        }
    }
//InterlockedクラスのIncrement/Decrementメソッドは、この1を足したり引いたりする動作がスレッドセーフであることを保証する。
int count = 0;
Interlocked.Increment(ref count); 

int count = 0;
Interlocked.Decrement(ref count); 

//Exchangeメソッドは、第1パラメータに指定された変数に対して、第2パラメータに指定された値を代入する動作をスレッドセーフで行う。メソッドは、第1パラメータに指定された変数の変更前の値を戻り値として返す。
int number = 0;
int originalNumber = 100;
Interlocked.Exchange(number, originalNumber);

//CompareExchangeメソッドは3つのパラメータを持つ。このメソッドは、第1パラメータと第3パラメータの値を比較し、値が等しければ第2パラメータの値を第1パラメータに代入するという動作を行う。戻り値は第1パラメータの変更前の値になる。
Interlocked.CompareExchange(x, y, null);