简体   繁体   English

同步块内部运行方法

[英]Synchronized Block inside run method

If I have something like below, so what does that mean inside synchronized block 如果我有类似下面的内容,那么在synchronized block内是什么意思

synchronised (syncObject) {

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) . 基本上,这意味着只有一个线程可以在上述块内,并且一旦一个线程执行完毕,第二个线程将进入该同步块synced(syncObject) Right? 对? Can anyone explain to me in a LayMan language so that I can get better picture? 谁能用LayMan语言向我解释,以便获得更好的照片?

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() {


}

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). 基本上,这意味着只有一个线程可以在上述块内,并且一旦一个线程执行完,第二个线程将进入已同步块(syncObject)。 Right? 对?

Right! 对!

So I need to make this method synchronized or not? 所以我需要使这种方法同步或不同步?

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. 假设只从run()方法中的同步块中调用该方法,则该块将已经防止多个线程同时执行该方法。 So declaring the method to be synchronized is redundant. 因此,声明要synchronized的方法是多余的。

However, I should point out some things: 但是,我应该指出一些事情:

  • When you declare an instance method as synchronized , it will synchronize on this ; 当您将实例方法声明为已synchronized ,它将在this方法上进行synchronized ie on the Task object. 即在Task对象上。 But your synchronized block is synchronizing on a different object ... the object in syncObject . 但是您的synchronized块正在另一个对象上同步... 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()方法中的synchronized块,则您会发现线程正在尝试在不同的对象上进行同步...并且您不会相互排斥。

  • 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. 通过在run()方法的顶层进行同步...对执行该任务的所有线程使用单个共享的syncObject ...您可以有效地使这些任务一次运行。 This completely negates any benefits of using threads. 这完全否定了使用线程的任何好处。

  • It is good practice to declare the variable containing a private lock object (such as syncObject ) to be final . 优良作法是将包含私有锁定对象(例如syncObject )的变量声明为final This avoids the possibility that something might overwrite it ... resulting in a synchronization failure. 这避免了某些东西可能覆盖它的可能性,从而导致同步失败。

No, attributeMethod is already running within the scope of a synchronized block; 否, attributeMethod已在synchronized块范围内运行; no need to mark it as such, unless you intend to call it concurrently outside this block. 除非您打算在此块之外同时调用它,否则无需将其标记为此类。

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

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