Handling Threading Issues

Source: Internet
Author: User

Avoid deadlocks:

Lock an object in the same order in all threads

Locks the lock object required in all calls to the same location in the code, releasing all objects immediately in the call to the uprising

Lock objects as little as possible and as short as possible

If you must lock multiple objects, make sure that all locks are successful and terminate them if they have not finished processing. The Monitor.TryEnter method can be used to do this, the following code

if (Monitor.TryEnter (employee))

{

if (Monitor.TryEnter (payroll))

{

Processing here

}

Else

Couldn ' t lock payroll

Monitor.Exit (employee);

}

------------------------------------------------

Avoiding race conditions

Keep the work that must be done sequentially in one thread, rather than spreading them to multiple threads

If a thread must be completed in another county before it can continue, use Thread.Join to force the second thread to wait for the first thread

If the snort is using a shared resource couple on a thread and the second thread can use the shared resource, use a synchronization object like monitor or mutex to control access to that resource

Handling Threading Issues

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.