简体   繁体   English

Java并发中的同步块/方法和调度规则

[英]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: 我在此处引用了有关调度规则的说明: http ://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fruntime_jobs_rules.htm规则如下:

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; 但是,Java在其预定义的互斥锁中使用监视器。 not semaphores. 不是信号量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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