简体   繁体   中英

Calling java timer in a synchronized block of codes

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 AI 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);

    } 

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.

块B将在A完成执行后执行,这是因为在方法A中,您只是将B调度为在2*60*1000毫之后运行,而计时器将在2*60*1000毫之后执行B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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