Code | Specification 4.4 Multi-thread multithreading
1. Application of synchronization mechanism space.
Use synchronization Domains. Chapter 8 in programming. NET components.
Avoid manual synchronization mechanisms that should lead to deadlocks and race conditions.
A) Avoid manual synchronization because that often leads to deadlocks and race.
2. Never call your sync mechanism space on the outside.
Never call outside your synchronization domain.
3. Using callback methods to control asynchronous calls
Manage asynchronous call completion in a callback method.
Do not wait, investigate or hinder completion.
A) Do don't wait, poll, or blocks for completion.
4. Always name the thread.
Always Name your threads.
Thread names can be tracked in a threaded debug form, so making a debug thread makes the program more efficient.
A) The Name is traced in the Debugger Threads window, making a debug sessions more productive.
Thread CurrentThread = Thread.CurrentThread;
String threadname = "Main UI Thread";
Currentthread.name = ThreadName;
5. Do not call suspend () or Resume () in the thread.
Don't call Suspend () or Resume () thread.
6. Do not call Thread.Sleep ()
Don't 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. Do not call thread.spinwait ()
Don't call Thread.spinwait ().
8. Do not call Thread.Abort () to end the thread.
Don't call Thread.Abort () to terminate threads.
Instead of the end of the tag thread, the synchronization object is applied to complete.
A synchronization object instead to signal with the thread to terminate. Chapter 8 in programming. NET components.
9. Avoid setting the priority of control execution by showing.
Avoid explicitly setting thread priority to control execution.
Priority can be set based on tasks, such as the thread used for screen memory should be below the normal level.
A) Can set thread priority based on task semantic, such as below normal for a-screen saver.
10. Do not read the value of the ThreadState property.
Do is read the value of the ThreadState property.
Application Method Thread.isalive () to determine whether the thread is invalid.
A) use Thread.isalive () to determine whether the Thread is dead.
11. At the end of the application, do not set the thread by setting the thread type.
Don't rely on setting the thread type to background thread for application shutdown.
You can use watchdog or other monitoring tools to resolutely end the thread.
A) use a watchdog or other monitoring entity to deterministically kill threads.
12. Do not apply the thread's local storage unless the thread's dependencies are guaranteed.
Do isn't use thread the local storage unless thread affinity is guaranteed.
13. Do not call Thread.memorybarrier ().
Don't call Thread.memorybarrier ().
14. Thread.Join () shall not be invoked until the inspection is done.
Never call Thread.Join () without checking this are not joining your Thread.
Void Waitforthreadtodie (thread thread)
{
Debug.Assert (Thread.CurrentThread.GetHashCode ()!= Thread. GetHashCode ());
Thread.Join ();
}
15. Always apply the lock () declaration instead of the obvious monitor manipulation.
Always Use the lock () statement rather than explicit Monitor manipulation.
16. Always place the lock () declaration in the object it protects.
Always encapsulate the Lock () statement inside the object it protects.
public class MyClass
{
public void DoSomething ()
{
Lock (This)
{...}
}
}
A You can use the Sync method to write the lock () declaration instead of yourself.
Can use synchronized methods instead of writing the lock () statement yourself.
17. Avoid decentralized locking.
Avoid fragmented locking (Chapter 8 of programming. NET components).
18. Avoid using the monitor to wait or stimulate the object. The event should be manually or automatically reset.
Avoid using a Monitor to or pulse objects. Use manual or Auto-reset events instead.
19. Do not use unstable variables. Lock objects or domains, rather than ensuring deterministic and thread-safe access.
Don't use volatile variables. Lock your object or fields instead to guarantee deterministic and thread-safe access.
Do not use Thread.volatileread (), thread.volatilewrite () or unstable modifier.
A) does not use Thread.volatileread (), Thread.volatilewrite () or the volatile modifier.
20. Never stack declare lock because it does not provide automatic locking. To use WaitHandle.WaitAll ().
Never stack lock statements because that does not provide automatic. Using WaitHandle.WaitAll () instead.
MyClass obj1 = new MyClass ();
MyClass obj2 = new MyClass ();
MyClass obj3 = new MyClass ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.