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

问题描述

如果我有类似下面的内容,那synchronized block

If I have something like below, so what does that mean inside synchronized block

synchronised (syncObject) {

基本上,这意味着只有一个线程可以在上述块中,一旦一个线程执行完,第二个线程将进入该同步块 synchronized(syncObject).正确的?谁能用LayMan语言向我解释,以便获得更好的照片?

Basically, it will means only one thread can be inside the above block and as soon as one thread is finished executing, second thread will enter that synchronized block synchronised (syncObject). Right?Can anyone explain to me in a LayMan language so that I can get better picture?

private static final class Task implements Runnable {
{
  private static Object syncObject = new Object();

    public Task(Command command, BlockingQueue<Integer> pool1, BlockingQueue<Integer> pool2) {
    this.command = command;
    this.existPool = pool1;
    this.newPool = pool2;
}

  public void run()
  {
    synchronised (syncObject) {
      if() {
        existId = existPool.take();
        attributeMethod(existId);
        } else if() {
            newId = newPool.take();
            attributeMethod(newId);
        }
    }
  }
}

// So I need to make this method synchronized or not? Currently I have made this synchronized
private synchronized void attributeMethod(int range) {
    // And suppose If I am calling any other method here-

 sampleMethod();
}


// What about this method, I need to make this synchronized as well? or not?
private synchronized void sampleMethod() {


}

推荐答案

对!

不,你不知道.假设只在run()方法中的同步块内调用该方法,则该块将已经阻止多个线程同时执行该方法.因此,将方法声明为synchronized是多余的.

No you don't. Assuming that the method is only going to be called from within the synchronized block in the run() method, that block will already prevent multiple threads from executing the method simultaneously. So declaring the method to be synchronized is redundant.

但是,我应该指出一些事情:

However, I should point out some things:

  • 当您将实例方法声明为synchronized时,它将在this上同步;即在Task对象上.但是您的synchronized块正在另一个对象上同步... ... syncObject中的对象.在这种情况下,这无关紧要.但是,如果run()方法中的synchronized块不存在,您会发现线程正在尝试在不同的对象上进行同步...并且您不会相互排斥.

  • When you declare an instance method as synchronized, it will synchronize on this; i.e. on the Task object. But your synchronized block is synchronizing on a different object ... the object in syncObject. In this case, this doesn't matter. However, if the synchronized block in the run() method wasn't there, you would find that the threads were attempting synchronizing on different objects ... and you would NOT get mutual exclusion.

通过在run()方法的顶层进行同步...对执行该任务的所有线程使用单个共享的syncObject ...您实际上使这些任务一次运行.这完全抵消了使用线程的任何好处.

By synchronizing at the top level of the run() method ... using a single shared syncObject for all threads that execute that task ... you are effectively making the tasks run one at a time. This completely negates any benefits of using threads.

优良作法是将包含私有锁对象(例如syncObject)的变量声明为final.这避免了某些东西可能覆盖它的可能性...导致同步失败.

It is good practice to declare the variable containing a private lock object (such as syncObject) to be final. This avoids the possibility that something might overwrite it ... resulting in a synchronization failure.

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

09-01 18:34