简体   繁体   中英

synchronized block/method and scheduling rules in java concurrency

I'm investigate about synchronized block and scheduling rules. I know that both methods are used to guarantee synchronous data. But I don't understand them, how they work. What are the advantage and disadvantage of synchronized and scheduling rules? I referd the instruction about scheduling rules here: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fruntime_jobs_rules.htm In this document, it represented about a rule as below:

We can fix this example by creating a simple scheduling rule that acts as a mutex (also known as a binary semaphore):

   class Mutex implements ISchedulingRule {
      public boolean isConflicting(ISchedulingRule rule) {
         return rule == this;
      }
      public boolean contains(ISchedulingRule rule) {
         return rule == this;
      }    }

Then, the rule is set into a object or method to control jobs. In this code, I don't see rule as well as how to check rule. And when is the scheduling rule or synchronized used?

Thank in advance

schedule and synchronization are used when threads need to access the same data. It would be a major problem if one thread reads data, mutates it, and before it can write the data back, another thread reads the data.

This cross-section needs to be carefully attended to to ensure only one process can access the shared resource at a time. Synchronization allows only one process at a time to utilize the resource (in a more advanced way, the synchronization can allow a pre-determined number of multiple accesses if so inclined; ie semaphores)

The schedule is used to address which order the threads can access (timing) the shared resource.

If a thread does not have access to a resource that another thread would, there is no reason to worry about synchronization as it is the only one using that resource

specific to your code snippet, it creates a mutex (which it clearly states) which allows access of the resource to only one process- the note above it calls it a binary semaphore, and for all intents-and-purposes, it works like one. However, java uses monitors in their pre-defined mutex locks; not semaphores.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM