ダメプログラマーのチラシウラ

スレッド

最終更新:

boatassist

- view
だれでも歓迎! 編集
非同期で同じインスタンス内の変数を操作
       private static Generater stagene;
       private void ExecThread()
       {
           stagene = new Generater();
           int cnt = 12;
           Thread[] thread = new Thread[cnt];
           for (int i = 0; i < cnt; i++)
               thread[i] = new Thread(this.ExceDo);
           for (int i = 0; i < cnt; i++)
               thread[i].Start();
           Thread.Sleep(300);
           Console.WriteLine(Generater.Count.ToString());
       }
       private void ExceDo()
       {
           Generater.Count++;
       }

対象クラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

   public class Generater
   {
       public static int Count;
       Generater s_Instance;
       public Generater()
       {
           s_Instance = this;
       }
   }
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

目安箱バナー