using System;
using System.Threading;
using System.Diagnostics;
namespace C17_8
{
public class Add
{
int sum;
public Add(int initial)
{
sum = initial;
}
public void DoAdd(int amout)
{
lock (this)
{
Console.WriteLine("Current Thread:" + Thread.CurrentThread.Name);
Console.WriteLine(sum);
Thread.Sleep(5);
sum = sum + amout;
}
}
}
public class DoTask
{
Add a = new Add(2);
public void DoWork()
{
for (int i = 0; i < 4; i++)
{
a.DoAdd(2);
}
}
}
public class Test
{
static Thread[] threads = new Thread[10];
static void Main()
{
DoTask dTask = new DoTask();
for(int i=0;i<10 i="" p=""> {
Thread t = new Thread(new ThreadStart(dTask.DoWork));
threads[i] = t;
}
for(int i=0;i<10 i="" p=""> {
threads[i].Name = i.ToString();
}
for(int i=0;i<10 i="" p=""> {
threads[i].Start();
}
}
}
}10>10>10>
沒有留言:
張貼留言