110,500
社区成员
发帖
与我相关
我的任务
分享
1: class ThreadTest
2: {
3: bool done;
4:
5: static void Main()
6: {
7: ThreadTest tt = new ThreadTest(); // Create a common instance
8: new Thread(tt.Go).Start();
9: tt.Go();
10: }
11:
12: // Note that Go is now an instance method
13: void Go()
14: {
15: if (!done) { done = true; Console.WriteLine("Done"); }
16: }
17: }
18:
19:
1: class ThreadTest
2: {
3: bool done;
4:
5: static void Main()
6: {
7: ThreadTest tt = new ThreadTest(); // Create a common instance
8: new Thread(tt.Go).Start();
9: tt.Go();
10: }
11:
12: // Note that Go is now an instance method
13: void Go()
14: {
15: if (!done)
{
Thread.Sleep(3000); //线程近来休眠 3秒;
done = true;
Console.WriteLine("Done");
}
16: }
17: }
using System;
using System.Threading;
class Test
{
static bool done = true;
static readonly object locker = new object();
static Test()
{
Console.WriteLine(done.ToString());
done = false;
Console.WriteLine(done.ToString());
}
static void Main()
{
Param i = new Param();
i.I = 0;
Thread aa = new Thread(new ParameterizedThreadStart(Test.Go));
aa.Start(i);
Param b = new Param();
b.I = 0;
Go(b);
Console.ReadKey();
}
static void Go(object i)
{
Param a=(Param)i;
a.I++;
lock (locker)
{
if (!done) { Console.WriteLine("{0}", a.I + " Done"); done = true; }
}
}
}
class Param {
private int i;
public int I{
get { return i; }
set { i = value; }
}
}