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.