日期:2013-04-06  浏览次数:20500 次

4.4多线程 Multithreading
1.应用同步机制空间。
Use Synchronization Domains. See Chapter 8 in Programming .NET Components.
避免手工的同步机制,应为这样容易导致死锁和竞态条件。
a) Avoid manual synchronization because that often leads to deadlocks and race conditions.
2.永远不要在外部调用你的同步机制空间。
Never call outside your synchronization domain.
3.用回调方法控制非同步调用
Manage asynchronous call completion on a callback method.
不要等待,调查 或者阻碍完成。
a) Do not wait, poll, or block for completion.
4.总是命名线程。
Always name your threads.
线程名字可以在线程调试窗体里跟踪,所以做一个调试调试线程会使程序变得更有效。
a)Name is traced in the debugger Threads window, making a debug session more productive.
Thread currentThread = Thread.CurrentThread;
String threadName = “Main UI Thread”;
currentThread.Name = threadName;
5.不要在线程中调用Suspend() 或者 Resume()
Do not call Suspend() or Resume() on a thread.
6.不要调用Thread.Sleep()
Do not call Thread.Sleep().
a)Thread.Sleep(0) is acceptable optimization technique to force a context switch.
b)Thread.Sleep() is acceptable in testing or simulation code.
7.不要调用Thread.SpinWait()
Do not call Thread.SpinWait().
8.不要调用Thread.Abort()来结束线程。
Do not call Thread.Abort() to terminate threads.
不是标记线程的结束,而是应用同步对象去完成。
a) Use a synchronization object instead to signal the thread to terminate. See Chapter 8 in Programming .NET Components.
9.避免显示地设定控制执行的优先权。
Avoid explicitly setting thread priority to control execution.
可以基于任务来设定优先权,例如用于屏幕存储器的线程应该低于一般水平。
a) Can set thread priority based on task semantic, such as below normal for a screen saver.
10.不要读取ThreadState属性的value值。
Do not read the value of the ThreadState property.
应用方法Thread.IsAlive()来判断线程是否失效。
a) Use Thread.IsAlive() to determine whether the thread is dead.
11.应用程序结束时,不要通过设置线程类型来设定线程。
Do not rely on setting the thread type to background thread for application shutdown.
可以用watchdog或其他监控工具来坚决的结束线程。
a) Use a watchdog or other monitoring entity to deterministically kill threads.
12.不要应用线程的本地存储,除非该线程的相关性已经得到保证。
Do not use thread local storage unless thread affinity is guaranteed.
13.不要调用Thread.MemoryBarrier()。
Do not call Thread.MemoryBarrier().
14.在未做检验之前万不可调用Thread.Join()。
Never call Thread.Join() without checking that you are not joining your own thread.
Void WaitForThreadToDie(Thread thread)
{
Debug.Assert(Thread.CurrentThread.GetHashCode() != thread.GetHashCode());
Thread.Join();
}
15.总是应用lock()声明,而不是用明显的Monitor manipulation。
Always use the lock() statement rather than explicit Monitor manipulation.
16.总是将lock()声明放在它所保护的对象里面。
Always encapsulate the lock() statement inside the object it protects.
Public class MyClass
{
public void DoSomething()
{
lock(this)
{…}
}
}
a)可以使用同步方法来代替你自己写lock()声明。
Can use synchronized methods instead of writing the lock() statement yourself.
17.避免分散锁定。
Avoid fragmented locking(see Chapter 8 of Programming .NET Components).
18.避免用监视器去等待或刺激对象。应该用手工的或自动重起事件。
Avoid using a Monitor to wait or pulse objects. Use manual or auto-reset events instead.
19.不要使用不稳定变量。锁定对象或域,而不是去保证决定性和线程安全访问。
Do not use volatile variables. Lock your object or fields instead to guarantee deterministic and thread-safe access.
不要使用Thread.VolatileRead(), Thread.VolatileWrite()或者不稳定修改器。
a) Do not use Thread.VolatileRead(), Thread.VolatileWrite() or the volatile modifier.
20.永远不要stack声明lock,因为这样不提供自动锁定。要使用WaitHandle.WaitAll()。
Never stack lock statements because that does not provide automatic locking. Using WaitHandle.WaitAll() instead.
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
MyClass obj3 = new MyClass();

//Do not stack lock statements
lock(obj1)
lock(obj2)
lock(obj3)
{
obj1.DoSomething();
obj1.DoSomething();
obj1.DoSomething();
}