using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SyncTest1
{
class Sync
{
int m_count = 0;
public int Count
{
get { return m_count; }
}
public void Count100()
{
for (int i = 0; i < 100; i++)
{
//Monitor.Enter(this);
//try
//{
lock(this)
{
int temp = m_count;
Thread.Sleep(1);
temp++;
//Interlocked.Increment(ref temp);
m_count = temp;
}
//}
//finally
//{
// Monitor.Exit(this);
//}
}
}
}
class Program
{
static void Main(string[] args)
{
Sync sync = new Sync();
Thread syncThread = new Thread(new ThreadStart(sync.Count100));
Thread syncThread2 = new Thread(new ThreadStart(sync.Count100));
syncThread.Start();
syncThread2.Start();
syncThread.Join();
syncThread2.Join();
Console.WriteLine("{0}까지 셌습니다",sync.Count);
}
}
}
'프로그래밍 > C#.net' 카테고리의 다른 글
WinService구동시 installutil문제 (2) | 2009.02.17 |
---|---|
[C#] AutoResetEvent 와 ManualResetEvnet 예제 (0) | 2009.01.15 |
Thread 5분연습 예제 (0) | 2009.01.15 |
[C#] ASCII로 파일입출력 하기 예제 (0) | 2009.01.15 |
[C#]String 클래스 사용법 예제 (0) | 2009.01.15 |
댓글