Threads are responsible for multitasking within a single application.You may never need to manage threads explicitly,because the CLR abstracts much of the thread support into classes that simplify most threading tasks.Even if you don`t create your own threads explicitly,you`ll ensure that your code can handle multiple threads if it`s run in a multithreading environment.The concern is especially important if you are creating components that mught be used by other programmers in a program that supports multithreading.It is particularly significant to remoting and web services developers.Although web services have many attributes of desktop applications,they are run on the server ,generally lack a user interface ,and force the developer to think about server-side issues such as efficiency and multithreading.
1.创建线程
CLR定义了ThreadStart委托,public delegate void ThreadStart();看代码
class Program { static void Main(string[] args) { new MultiThreading().DoTest(); } } class MultiThreading { public void DoTest() { Thread t1 = new Thread( new ThreadStart(count) ); Thread t2 = new Thread(new ThreadStart(reverseCount)); t1.Start(); t2.Start(); } public void count() { for (int i = 0; i < 1000; i++) Console.WriteLine("++ "+ i); } public void reverseCount() { for (int i = 999; i >= 0; i--) Console.WriteLine("-- "+ i); } }
以前我看见过一些代码,有的要求必须主线程结束之后,新创建的线程才能结束,但是在这里,很明显主线程应该早已结束,但是为什么新创建的线程还能继续运行呢?原来线程分为前台线程和后台线程。这两种线程的区别是:当前台程序全部结束时,程序结束。此时不管后台程序有没有完毕,通通结束,也就是说前台线程是决定程序结束的关键,后台线程没有决定权,当前台线程通通结束时,他们就得结束。 通过IsBackground属性可以查看或者设置线程的前后台情况。我们通过Thread创建的线程默认情况下是前台的,所以程序会等我们的线程通通运行结束才会结束,但是下面的例子就不行了,你会发现新创建的线程根本没有运行:因为通过BeginInvoke调用的线程都是后台线程,前台线程main一结束,整个程序就结束了。
class Program { static void Main(string[] args) { MultiThreading mul = new MultiThreading(); mul.fun = mul.testEvent; mul.fun.BeginInvoke(null, null); } } class MultiThreading { public Fun fun; public void testEvent() { for (int i = 0; i < 10000; i++) Console.WriteLine("xuxu"); } } public delegate void Fun();
2.关于Join-直到join的线程运行结束才往下运行.例如下面的例子,开始是main和DoTest中的thread并行运行,当beforemain运行结束后,Join的线程把main挂起,自己和thread并行运行,直到自己运行结束,main才继续得以运行。这相当于是一个插队,暂时取代别人把自己当做和外面线程并行运行的代表。
class Program { static void Main(string[] args) { MultiThreading mul = new MultiThreading(); mul.DoTest(); for (int i = 0; i < 50; i++) Console.WriteLine("before main "+i); Thread thread = new Thread(mul.fun1); thread.Start(); thread.Join(); for (int i = 0; i < 10; i++) Console.WriteLine("after main"); } } class MultiThreading { public void fun() { for (int i = 0; i < 100; i++) { Console.WriteLine("++++ " + i); } } public void fun1() { for (int i = 99; i >= 0; i--) Console.WriteLine("---- " + i); } public void DoTest() { Thread thread = new Thread(fun); thread.Start(); } }
3.终止线程-这里的join是为了不想主函数的writeline在线程结束之前运行。
using System; using System.Threading; public class Worker { // This method will be called when the thread is started. public void DoWork() { while (!_shouldStop) { Console.WriteLine("worker thread: working..."); } Console.WriteLine("worker thread: terminating gracefully."); } public void RequestStop() { _shouldStop = true; } // Volatile is used as hint to the compiler that this data // member will be accessed by multiple threads. private volatile bool _shouldStop; } public class WorkerThreadExample { static void Main() { // Create the thread object. This does not start the thread. Worker workerObject = new Worker(); Thread workerThread = new Thread(workerObject.DoWork); // Start the worker thread. workerThread.Start(); Console.WriteLine("main thread: Starting worker thread..."); // Loop until worker thread activates. while (!workerThread.IsAlive); // Put the main thread to sleep for 1 millisecond to // allow the worker thread to do some work: Thread.Sleep(1); // Request that the worker thread stop itself: workerObject.RequestStop(); // Use the Join method to block the current thread // until the object's thread terminates. workerThread.Join(); Console.WriteLine("main thread: Worker thread has terminated."); } }
4.同步-看如下代码
class Program { static void Main(string[] args) { MultiThreading mul = new MultiThreading(); mul.DoTest(); } } class MultiThreading { private int count; public void increment() { while (count < 1000) { count++; Console.WriteLine(count); } } public void DoTest() { Thread thread = new Thread(increment); Thread thread1 = new Thread(increment); thread.Start(); thread1.Start(); thread.Join(); thread1.Join(); Console.WriteLine("All jobs done"); } }
使用Interlocked,还是不对
class Program { static void Main(string[] args) { MultiThreading mul = new MultiThreading(); mul.DoTest(); } } class MultiThreading { private int count; public void increment() { while (count < 1000) { int temp = Interlocked.Increment(ref count); Console.WriteLine(temp); } } public void DoTest() { Thread thread = new Thread(increment); Thread thread1 = new Thread(increment); thread.Start(); thread1.Start(); thread.Join(); thread1.Join(); Console.WriteLine("All jobs done"); } }
问题慢慢分析吧。。
这里我有一个疑问?
int temp = (a+1);是有几条语句