using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
class Program
{
public static void myStaticThreadMethod()
{
Console.WriteLine("myStaticThreadMethod");
}
static void Main(string[] args)
{
Thread thread1 = new Thread(myStaticThreadMethod);
thread1.Start(); // 只要使用Start方法,线程才会运行
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
class Program
{
public void myThreadMethod()
{
Console.WriteLine("myThreadMethod");
}
static void Main(string[] args)
{
Thread thread2 = new Thread(new Program().myThreadMethod);
thread2.Start();
}
}
}
如果读者的方法很简单,或出去某种目的,也可以通过匿名委托或Lambda表达式来为Thread的构造方法赋值,代码如下:
thread3.Start();
Thread thread4 = new Thread(( ) => { Console.WriteLine("Lambda表达式"); });
thread4.Start();
= "我的Lamdba";
thread5.Start();

我们可以将Thread类封装在一个MyThread类中,以使任何从MyThread继承的类都具有多线程能力。MyThread类的代码如下:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
abstract class MyThread
{
Thread thread = null;
abstract public void run();
public void start()
{
if (thread == null)
thread = new Thread(run);
thread.Start();
}
}
}
class NewThread : MyThread
{
override public void run()
{
Console.WriteLine("使用MyThread建立并运行线程");
}
}
static void Main(string[] args)
{
NewThread nt = new NewThread();
nt.start();
}
[ComVisibleAttribute(false)]
下面的代码使用了这个带参数的委托向线程传递一个字符串参数:
{
Console.WriteLine(obj);
}
static void Main(string[] args)
{
Thread thread = new Thread(myStaticParamThreadMethod);
thread.Start("通过委托的参数传值");
}
{
private String d1;
private int d2;
public MyData(String d1, int d2)
{
this.d1 = d1;
this.d2 = d2;
}
public void threadMethod()
{
Console.WriteLine(d1);
Console.WriteLine(d2);
}
}
MyData myData = new MyData("abcd",1234);
Thread thread = new Thread(myData.threadMethod);
thread.Start();
如果使用在第二节定义的MyThread类,传递参数会显示更简单,代码如下:
{
private String p1;
private int p2;
public NewThread(String p1, int p2)
{
this.p1 = p1;
this.p2 = p2;
}
override public void run()
{
Console.WriteLine(p1);
Console.WriteLine(p2);
}
}
NewThread newThread = new NewThread("hello world", 4321);
newThread.start();
{
Thread.Sleep(3000);
}
Thread thread = new Thread(myStaticThreadMethod);
// thread.IsBackground = true;
thread.Start();
{
private static int count = 0;
private int ms;
private static void increment()
{
lock (typeof(ThreadCounter)) // 必须同步计数器
{
count++;
}
}
private static void decrease()
{
lock (typeof(ThreadCounter))
{
count--;
}
}
private static int getCount()
{
lock (typeof(ThreadCounter))
{
return count;
}
}
public ThreadCounter(int ms)
{
this.ms = ms;
}
override public void run()
{
increment();
Thread.Sleep(ms);
Console.WriteLine(ms.ToString()+"毫秒任务结束");
decrease();
if (getCount() == 0)
Console.WriteLine("所有任务结束");
}
}
ThreadCounter counter1 = new ThreadCounter(3000);
ThreadCounter counter2 = new ThreadCounter(5000);
ThreadCounter counter3 = new ThreadCounter(7000);
counter1.start();
counter2.start();
counter3.start();
上面的代码虽然在大多数的时候可以正常工作,但却存在一个隐患,就是如果某个线程,假设是counter1,在运行后,由于某些原因,其他的线程并未运行,在这种情况下,在counter1运行完后,仍然可以显示出“所有任务结束”的提示信息,但是counter2和counter3还并未运行。为了消除这个隐患,可以将increment方法从run中移除,将其放到ThreadCounter的构造方法中,在这时,increment方法中的lock也可以去掉了。代码如:
public ThreadCounter(int ms)
{
this.ms = ms;
increment();
}

{
Thread.Sleep(Int32.Parse(obj.ToString()));
Console.WriteLine(obj + "毫秒任务结束");
}
private static void joinAllThread(object obj)
{
Thread[] threads = obj as Thread[];
foreach (Thread t in threads)
t.Join();
Console.WriteLine("所有的线程结束");
}
static void Main(string[] args)
{
Thread thread1 = new Thread(threadMethod);
Thread thread2 = new Thread(threadMethod);
Thread thread3 = new Thread(threadMethod);
thread1.Start(3000);
thread2.Start(5000);
thread3.Start(7000);
Thread joinThread = new Thread(joinAllThread);
joinThread.Start(new Thread[] { thread1, thread2, thread3 });
}

















