0

If I have a parent block of codes called A, A is synchronized. And in A, I executes a child block of code called B. Am I right to assume that B will be synchronized also?


If in A I have a timer to delay the execution of B in a certain of t time, is there a chance that B gets executed later when A already finished?


Thank you very much.


P/S: Sorry the the unclear code, this is how it should look like

   synchronized  A{
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        B block
      }
    }, 2*60*1000);

    } 
Xitrum
  • 7,765
  • 26
  • 90
  • 126
  • 2
    It is difficult to understand what you mean. Can you illustrate your question with some code? – Keppil Jul 02 '15 at 12:45
  • I think Block `B` would also be synchronous. I believe, in java, all code is executed synchronously by default. If `B` is contained in `A`, and `A` and `B` are executed synchronously, then `A` will not finish until after `B` finishes. – Xion Dark Jul 02 '15 at 12:46

2 Answers2

2

Depends. If the block B is a block of code within method like this, then yes... it will be synchronized.

public synchronized void methodA() {
    // Block B
    {
       // some code
    }
}

If it's another method like the following, then no:

public synchronized void methodA() {
    methodB();
}

public void methodB() {
    // Block B code
    // Nothing prevents an unsynchronized method from calling this method
    //   at same time as methodA() holds  lock on `this` object
}

Unless methodB is also marked synchronized or called from another synchronized method (eg/ public synchronized methodC()) or another synchronization mechanism is used, then methodB() is not synchronized.

Those are just the simplest cases. You're better off posting example code since 'block' is not well defined by default and the type of synchronization lock (implicit on this via synchronized method vs. explicit object lock) makes a difference.

But, your last line sounds like you're asking about synchronous vs. asynchronous execution of code which, while related to threading and synchronized blocks, is a different concept.

In that case, it depends on what happens in block A ... if new threads are created to execute block B then anything can happen with the timing of code execution. If no threads are created, it's safe to assume that block A will not complete before block B.

Edit: based on code now posted ... the synchronized A block will only ensure the Timer threads get created one at a time. But, unless there's something special about the Java Timer framework, there's nothing there that will prevent two threads from executing the B block in the run method simultaneously ... so make sure that the contents are thread safe.

That is, don't assume that just because different instances of Timer are created in a synchronized block with the same delay, they won't get into the run method at the same time. If the B block accesses external un-thread-safe code (eg. static methods, disk access), you could have surprises.

As Amm Sokun mentioned in other answer, A{} will return before Block B executes.

Jacob Zwiers
  • 1,092
  • 1
  • 13
  • 33
1

Block B will be executed after A has finished executing, this is because in method A you are just scheduling B to be run after 2*60*1000 milli, and timer will take care of executing B after 2*60*1000 milli

Amm Sokun
  • 1,298
  • 4
  • 20
  • 35