Java 同步块 [英] Java synchronized blocks

查看:54
本文介绍了Java 同步块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有一个方法:

public void doSomething(){
    synchronized(this){
        //some code processing here
    }
    String temp = "init"; //instead of i++
    synchronized(this){
        //some other code processing here
    }
}

这个方法等价于public synchronized void doSomething()吗?

是否有任何理由假设线程调度程序在某些执行中不会有效地导致与同步整个函数相同的流程?即:

Is there any reason not to assume that the thread scheduler in some executions would not result in effectively the same flow as synchronizing the whole function? That is:

  • Thread1 进入第一个同步块.
  • Thread2 块.
  • Thread1 继续 i++ 并移动到第二个同步块,而 Thread2 保持阻塞状态.
  • 因此,Thread2Thread1 退出两个同步块后进入该方法.
  • Thread1 enters the first synchronized block.
  • Thread2 blocks.
  • Thread1 continues with i++ and moves to the second synchronized block while Thread2 remains blocked.
  • As a result, Thread2 enters the method after Thread1 has exited both synchronized blocks.

我只需要知道:

  • 我能否指望两个线程(Thread1Thread2)可以同时在方法中的所有执行上下文?例如,第一个同步块中的 Thread2 和第二个同步块中的 Thread1 来实现并发.
  • 会不会有一些执行流程中只有一个线程(一次)在方法中有效地序列化整个流程,使其等同于 public synchronized void doSomething()?
  • Can I count on all execution contexts that both threads (Thread1 and Thread2) can be in the method at the same time? For example, Thread2 in the first sync block and Thread1 in the second sync block to achieve concurrency.
  • Will there be some execution flows where only one thread will be in the method (at a time) effectively serializing the whole flow, making it equivalent to public synchronized void doSomething()?

推荐答案

某些 执行中,它会与同步整个功能具有相同的流程,当然 - 但它真正等同于使方法已同步,所有 执行必须具有相同的流程.

In some executions it would have the same flow as synchronizing the whole functions, sure - but for it to be truly equivalent to making the method synchronized, it would have to have the same flow for all executions.

实际上,另一个线程可能会在执行到一半时获取锁(无论是针对此方法还是同一监视器上的其他代码锁定).如果方法本身是同步的,则不会发生这种情况,因此它们不等效.

As it is, there's a possibility that another thread will grab the lock (whether for this method or some other code locking on the same monitor) half way through execution. That couldn't happen if the method itself were synchronized, therefore they're not equivalent.

(顺便说一句,无论如何,锁定 this 通常被认为是不好的做法;我不记得我上次编写同步方法是什么时候.我改为锁定私人持有的监视器,所以我知道我的代码是唯一可以锁定它们的代码.)

(As an aside, locking on this is generally considered to be bad practice anyway; I can't remember the last time I wrote a synchronized method. I lock on privately held monitors instead, so that I know my code is the only code which can possibly lock on them.)

回复您的

我只需要知道我是否可以依靠所有执行上下文两个线程(例如 Thread1 和Thread2) 可以在方法中同时,例如第一个中的 thread2同步块和线程 1 在第二个同步块实现并发

All I need to know is whether I can count on all execution contexts that both threads (e.g. Thread1 and Thread2) can be in the method at the same time, e.g thread2 in the first sync block and thread1 in the second sync block to achieve concurrency

绝对不会!保证您不会在同一个监视器上同步的同步块中有两个线程.

Absolutely not! It's guaranteed that you won't have two threads both in a synchronized block synchronized on the same monitor.

您有三部分代码:第一个同步块、未同步部分和第二个同步部分.

You have three sections of code: the first synchronized block, the unsynchronized part, and the second synchronized part.

一次可以在未同步部分执行任意数量的线程.对于任何一个实例(因为您在 this 上同步)只有一个线程可以执行 任一 同步块.如果要实现并发,则必须在不同的监视器上进行同步.

Any number of threads can be executing in the unsynchronized part at a time. For any one instance (because you're synchronizing on this) only one thread can be executing either of the synchronized blocks. If you want to achieve concurrency, you'd have to synchronize on different monitors.

此外,听起来您想要保证调度程序让另一个线程在等待它时获取锁.我不相信有任何这样的保证 - 执行第一个块的线程可以释放锁,但会在相同的时间片中继续并在任何其他线程进入之前重新获取它.在某些 JVM 中可能不会发生,但我没有不相信有任何保证.

Furthermore, it sounds like you want guarantees of the scheduler letting another thread grab the lock if it was waiting for it. I don't believe there's any such guarantee - a thread executing the first block could release the lock but continue in the same timeslice and re-acquire it before any other threads got in. In some JVMs that may not happen, but I don't believe there's any guarantee around it.

这篇关于Java 同步块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆