前些天出差事情比较少就读了一下《C# Threading Handbook》,感觉还是收获不少。现在写写自己的体会,希望看到更好的评论,抛砖引玉吧。 该书内容涉及广泛,我只写一些自己认为重要但是自己以前不知道的地方吧。
1、线程的生命周期 System.Threading.Thread类提供了我们start,stop,suspend,resume,join,abort线程的方法。可以通过System.Threading.ThreadState属性来查看线程的状态。
The Suspend() method will suspend the current thread indefinitely until another thread wakes it up. The effect of calling Join() method is that the thread will be blocked until either the other thread completes or the time period elapses, whichever occurs first. The Abort() method would be very useful, if you want to terminate the thread for whatever reason.
2、适合与不适合使用线程地方 适合使用线程的机会: a. Background Processes The first opportunity to spawn a new thread occurs when your application needs to run a large process in the background while still keeping its user interface active and usable. b. Accessing external resources The second circumstance in which you might want to consider spawning a new thread occurs when you are accessing resources that are not local to your system. 不适合多线程的场合: The first is an instance where execution order is extremely important, and the second is a mistake seen quite often in code - creating new threads in a loop.
|
请发表评论