static void threadFunc(object inst) { MultiThreadWarning2 pg = inst as MultiThreadWarning2; for (int i = 0; i < 10000; i++) { pg.number = pg.number + 1; //MultiThreadWarning2 객체의 number필드 값 증가 } }
static void Main(string[] args) { MultiThreadWarning2 pg = new MultiThreadWarning2(); Thread t1 = new Thread(threadFunc); Thread t2 = new Thread(threadFunc); //2개의 스레드 시작 t1.Start(pg); t2.Start(pg); //2개의 스레드 실행이 끝날 떄 까지 대기 t1.Join(); t2.Join(); Console.WriteLine(pg.number); //스레드 실행 완료 후 number 필드 값 출력 }