Solve the problem of multi-threaded reading and writing the same file.

Source: Internet
Author: User
When multiple threads share an object Code Similarly, the lock keyword should not be used for this problem. Here we need to use system. threading.
A Class of Monitor, which can be called a monitor, provides a solution to share resources with threads.
The monitor class can lock an object. A thread can operate on this object only when this lock is obtained. The object lock mechanism ensures the next time in the case of confusion
Only one thread can access this object. Monitor must be associated with a specific object, but because it is a static class, it cannot be used to define the object, and it
All methods are static and cannot be referenced by objects. The following code uses monitor to lock an object:

......
Queue oqueue = new Queue ();
......
Monitor. Enter (oqueue );
... // Now the oqueue object can only be manipulated by the current thread
Monitor. Exit (oqueue); // release the lock

As shown above, when a thread calls the monitor. Enter () method to lock an object, this object will be owned by it. Other threads want to access this object, only waiting for it to make
Use the monitor. Exit () method to release the lock. To ensure that the thread can finally release the lock, you can write the monitor. Exit () method into the finally code block in the try-catch-Finally structure.
. For any monitor-locked object, some information related to it is stored in the memory. One is the reference of the thread currently holding the lock, and the other is a reserve team column.
The column stores the thread that is ready to get the lock. The third is a waiting queue, which stores the reference of the queue that is currently waiting for this object to change its status. If you have an object lock
When the thread is preparing to release the lock, it uses the monitor. Pulse () method to notify the first thread in the waiting queue, so the thread is transferred to the reserve queue column. When the object lock is released
The thread in the standby queue can immediately obtain the object lock.

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.