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());
}
}
}
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());
}
}
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());
}
}
}
}
int count = 0;
Interlocked.Increment(ref count);
int count = 0;
Interlocked.Decrement(ref count);
int number = 0;
int originalNumber = 100;
Interlocked.Exchange(number, originalNumber);
Interlocked.CompareExchange(x, y, null);