简体   繁体   English

Java中的同步块如何工作? 变量引用或内存被阻止?

[英]How synchronized Block In Java works? Variable reference or memory is blocked?

I have a situation and I need some advice about synchronized block in Java. 我有一种情况,我需要一些有关Java synchronized块的建议。 I have a Class Test below: 我下面有一个班级考试:

Class Test{
    private A a;
    public void doSomething1(String input){
        synchronized (a) {
            result = a.process(input);
        }
    }
    public void doSomething2(String input){
        synchronized (a) {
            result = a.process(input);
        }
    }
    public void doSomething3(String input){
            result = a.process(input);
    }
}

What I want is when multi threads call methods doSomeThing1() or doSomeThing2() , object "a" will be used and shared among multi threads (it have to be) and it only processes one input at a time (waiting until others thread set object "a" free) and when doSomeThing3 is called, the input is processed immediately. 我想要的是当多线程调用方法doSomeThing1()doSomeThing2() ,将在多线程之间使用并共享对象“ a”(必须是),并且一次只能处理一个输入(等待直到其他线程设置)对象“ a”免费),并在doSomeThing3时立即处理输入。

My question is will the method doSomeThing3() be impacted my method doSomeThing1() and doSomeThing2() ? 我的问题是方法doSomeThing3()会影响方法doSomeThing1()doSomeThing2() Will it have to wait if doSomeThing1() and doSomeThing2() are using object "a"? 如果doSomeThing1()doSomeThing2()使用对象“ a”,是否需要等待?

A method is never impacted by anything that your threads do. 方法永远不会受到线程执行的任何操作的影响。 What gets impacted is data , and the answer to your question depends entirely on what data are updated (if any) inside the a.process() call. 受影响的是数据 ,问题的答案完全取决于a.process()调用中更新的数据(如果有)。

You asked "Variable reference or memory is blocked?" 您问“变量引用或内存被阻止?”

First of all, "variable" and "memory" are the same thing. 首先,“变量”和“内存”是同一回事。 Variables, and fields and objects are all higher level abstractions that are built on top of the lower-level idea of "memory". 变量,字段和对象都是基于“内存”的较低层概念构建的较高层抽象。

Second of all, No. Locking an object does not prevent other threads from accessing or modifying the object or, from accessing or modifying anything else. 第二,否。锁定对象并不能阻止其他线程访问或修改该对象,也不能阻止其他线程访问或修改其他任何东西。

Locking an object does two things: It prevents other threads from locking the same object at the same time, and it makes certain guarantees about the visibility of memory updates. 锁定对象有两件事:防止其他线程同时锁定同一对象,并且对内存更新的可见性做出某些保证。 The simple explanation is, if thread X updates some variables and then releases a lock, thread Y will be guaranteed to see the updates only after it has acquired the same lock. 简单的解释是,如果线程X更新了一些变量然后释放了锁,则线程Y将仅在获得相同的锁之后才能保证看到更新。

What that means for your example is, if thread X calls doSomething1() and modifies the object a; 对于您的示例,这意味着,如果线程X调用doSomething1()并修改了对象a; and then thread Y later calls doSomething3(), thread Y is not guaranteed to see the the updates. 然后线程Y稍后调用doSomething3(),则不能保证线程Y会看到更新。 It might see the a object in its original state, it might see it in the fully updated state, or it might see it in some invalid half-way state. 它可能会看到一个对象处于其原始状态,它可能会看到它处于完全更新的状态,或者它可能会看到某个无效的中途状态。 The reason why is because, even though thread X locked the object, modified it, and then released the lock; 原因是,即使线程X锁定了该对象,也对其进行了修改,然后释放了该锁定; thread Y never locked the same object. 线程Y从未锁定同一对象。

In your code, doSomething3() can proceed in parallel with doSomething1() or doSomething2(), so in that sense it does what you want. 在您的代码中,doSomething3()可以与doSomething1()或doSomething2()并行进行,因此从某种意义上说,它可以满足您的需求。 However, depending on exactly what a.process() does, this may cause a race condition and corrupt data. 但是,取决于a.process()的确切作用,这可能会导致争用条件和数据损坏。 Note that even if doSomething3() is called, any calls to doSomething1() or doSomething2() that have started will continue; 请注意,即使已调用doSomething3(),已经开始的对doSomething1()或doSomething2()的任何调用都将继续; they won't be put in abeyance while doSomething3() is processed. 在处理doSomething3()时,它们不会被搁置。

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

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