“隐式” java中的同步?

发布于 2024-09-15 13:53:52 字数 226 浏览 3 评论 0原文

当我有这样的方法时,

public void unsynchronizedMethod(){
  callSynchronizedMethod();
  //dosomestuff after this line
}

这是否意味着在 unsynchronizedMethod() 方法中调用 callSynchronizedMethod() 后的所有内容都是隐式同步的?

when I have a method like

public void unsynchronizedMethod(){
  callSynchronizedMethod();
  //dosomestuff after this line
}

does it mean that all content, after calling callSynchronizedMethod() in the unsynchronizedMethod()-Method, is implicitly synchronized?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相思故 2024-09-22 13:53:52

不会。锁会在 callSynchronizedMethod() 结束时释放。

No. The lock is released at the end of callSynchronizedMethod().

所谓喜欢 2024-09-22 13:53:52

同步以简单的方式定义:

  • 对于 synchronized 方法:在该方法的执行中
  • 对于 synchronized 块:在该块的执行中

以下是相关摘录Java 语言规范第三版

JLS 14.19 同步声明

同步语句:

  1. 代表执行线程获取互斥锁,
  2. 执行一个块,
  3. 然后释放锁。

当执行线程拥有锁时,其他线程无法获取该锁。

synchronized 方法在语义上与应用于整个方法的 synchronized 语句相同 (§JLS 8.4.3.6。锁可以从 this 获取(如果它是实例方法)或 与方法的类关联的对象(如果它是静态方法;您不能在静态上下文中引用此)

。回答原来的问题,给出这个片段:

public void unsynchronizedMethod(){
  callSynchronizedMethod();

  doSomeUnsynchronizedStuff(); // no "implicit synchronization" here
}

仅与关键部分的同步,早期同步不会产生任何挥之不去的影响。

是设计使然:您应该始终努力最大限度地减少

  • 请注意,这 Java 第 2 版,第 67 项:避免过度同步

相关问题

Synchronization is defined in a straightforward manner:

  • For a synchronized method: within the execution of that method
  • For a synchronized block: within the execution of that block

Here are the relevant excerpts from Java Language Specification 3rd Edition:

JLS 14.19 The synchronized Statement

A synchronized statement:

  1. acquires a mutual-exclusion lock on behalf of the executing thread,
  2. executes a block,
  3. then releases the lock.

While the executing thread owns the lock, no other thread may acquire the lock.

synchronized methods is semantically identical to a synchronized statement applied to the whole method (§JLS 8.4.3.6. The lock is obtained from either this (if it's an instance method) or the Class object associated with the method's class (if it's a static method; you can't refer to this in a static context).

So to answer the original question, given this snippet:

public void unsynchronizedMethod(){
  callSynchronizedMethod();

  doSomeUnsynchronizedStuff(); // no "implicit synchronization" here
}

Note that this is by design: you should always strive to minimize synchronization to only critical sections. Outside of those critical sections, there is no lingering effect from earlier synchronization.

See also

  • Effective Java 2nd Edition, Item 67: Avoid excessive synchronization

Related questions

屌丝范 2024-09-22 13:53:52

不,不是。

同步意味着一次只有一个线程执行代码。

但是在 callSynchronizedMethod() 之后,线程可以同时以不同的顺序再次运行代码。

No, it's not.

Synchronized means that only one thread at a time will execute the code.

But after callSynchronizedMethod() the threads could run again over the code in different order, all at at the same time.

十二 2024-09-22 13:53:52

不,不会有隐式同步。同步在块或函数范围内工作。同步块之外的任何内容都不会同步。

下面的示例代码表明了这一点。如果方法同步,它将始终打印 0。

class example extends Thread{
   //Global value updated by the example threads
   public static volatile int value= 0;
    public void run(){
    while(true)//run forever
       unsynchMethod();

   }
   public void unsynchMethod(){
     synchronizedMethod();
    //Count value up and then back down to 0
    for(int i =0; i < 20000;++i)
       value++;//reads increments and updates value (3 steps)
    for(int i = 0; i < 20000;++i)
       value--;//reads decrements and updates value (3 steps)
      //Print value 
      System.out.println(value);
   }
   //Classlevel synchronized function
   public static synchronized void synchronizedMethod(){
      //not important
   }
   public static void main(String... args){
    example a = new example();
    example b = new example();
    a.start();
    b.start();
   }

}

如果同步,我的结果应该为 0:

4463
6539
-313
-2401
-3012
...

No, there wont be an implicit synchronisation. Synchronized works within a block or function scope. Anything outside a synchronized block is not synchronized.

The following example code shows that. If the methods where synchronized it would always print 0.

class example extends Thread{
   //Global value updated by the example threads
   public static volatile int value= 0;
    public void run(){
    while(true)//run forever
       unsynchMethod();

   }
   public void unsynchMethod(){
     synchronizedMethod();
    //Count value up and then back down to 0
    for(int i =0; i < 20000;++i)
       value++;//reads increments and updates value (3 steps)
    for(int i = 0; i < 20000;++i)
       value--;//reads decrements and updates value (3 steps)
      //Print value 
      System.out.println(value);
   }
   //Classlevel synchronized function
   public static synchronized void synchronizedMethod(){
      //not important
   }
   public static void main(String... args){
    example a = new example();
    example b = new example();
    a.start();
    b.start();
   }

}

My Results, should have been 0 if synchronized:

4463
6539
-313
-2401
-3012
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文