简体   繁体   English

在同步代码块中调用Java Timer

[英]Calling java timer in a synchronized block of codes

If I have a parent block of codes called A, A is synchronized. 如果我有一个称为A的父代码块,则A被同步。 And in A, I executes a child block of code called B. Am I right to assume that B will be synchronized also? 在A中,我执行了一个称为B的代码子块。我是否也认为B也将被同步?


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? 如果在AI中有一个计时器在一定的t时间内延迟B的执行,那么当A已经完成时,是否有可能B在以后执行?


Thank you very much. 非常感谢你。


P/S: Sorry the the unclear code, this is how it should look like P / S:对不起,不清楚的代码,这就是它的外观

   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. 如果块B是这种方法中的代码块,则可以...它将被同步。

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. 除非methodB也被标记为已synchronized 已从另一个同步方法(例如, public synchronized methodC()public synchronized methodC() 或使用了另一个同步机制,否则methodB()将不会同步。

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. 你已经开发布示例的代码,因为“块”没有很好默认定义和同步锁的类型(关于隐式更好this通过同步方法与明确的对象锁)有差别。

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. 但是,您的最后一行听起来像是您在询问同步执行代码还是异步执行代码,尽管与线程和synchronized块有关,但这是一个不同的概念。

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. 在那种情况下,这取决于在block A发生的事情……如果创建了新的线程来执行block B那么在代码执行的时间上可能发生任何事情。 If no threads are created, it's safe to assume that block A will not complete before block B . 如果没有创建线程,则可以安全地假设block Ablock B之前不会完成。

Edit: based on code now posted ... the synchronized A block will only ensure the Timer threads get created one at a time. 编辑:基于现在发布的代码...同步的A block将仅确保一次创建Timer线程。 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. 但是,除非Java Timer框架有什么特别之处,否则没有什么可以阻止两个线程在run方法中同时执行B block的…所以确保内容是线程安全的。

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. 也就是说,不要仅仅因为在相同的延迟下在同步块中创建了Timer不同实例,就不会假设它们不会同时进入run方法。 If the B block accesses external un-thread-safe code (eg. static methods, disk access), you could have surprises. 如果B block访问外部的非线程安全代码(例如,静态方法,磁盘访问),您可能会感到惊讶。

As Amm Sokun mentioned in other answer, A{} will return before Block B executes. 正如Amm Sokun在其他答案中提到的那样, A{}将在Block B执行之前返回。

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

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

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