日期:2014-05-17 浏览次数:20747 次
public class shareMemory
{
public int Memory { get; set; }
}
public class increaseMemory
{
private shareMemory test;
public increaseMemory(shareMemory obj)
{
this.test = obj;
}
public void increase()
{
for (int i = 0; i < 100000; i++)
{
test.Memory++;
}
}
}
class Program
{
static void Main(string[] args)
{
shareMemory obj = new shareMemory();
Task[] tasks = new Task[5];
for (int i = 0; i < 5; i++)
{
tasks[i] = new Task(new increaseMemory(obj).increase);
tasks[i].Start();
}
for (int i = 0; i < 5; i++)
{
tasks[i].Wait();
}
Console.WriteLine(obj.Memory);
Console.ReadKey();
}
}