English 中文(简体)
停止使用一种方法,直到再次使用。
原标题:Pause a method until it gets called again

我有这一方法<代码>run()执行许多其他方法。 在这些次方法中,有一些次方法中含有1种对应方法。 当该反射达到一定编号n时,应暂停使用run()这一方法。 当它再次发出呼吁时,它应当开始在暂停之前执行。

是否可在 Java?

<>Edit:根据Andersoj的要求提供更多信息

我试图 strip倒我的法典。 这是它所期望的非常基本的例子。

public void run(int n) {
   executeA();
}

public void executeA() {
   while (randomCondition) {
      executeB();
      counter++;
   }
   executeC();
   if (counter <= 100)
      executeD();
}

<代码>executeA(>>中的代码应运行到counter。 平等<代码>n。 只有在执行之前,它才会暂停使用(可在<条码>executeA(任何地点发生)。

After a while, run() is called again, the code should resume where the thread previously stopped in executeA().

再次感谢!

问题回答

如果您的停职和复职是基于某种条件,并假定该方法会因其他一些可能改变条件(贵方不明白)的透镜而消失:

public void run() {
   synchronized(mutex){
      while(condition){
         try {
            mutex.wait();
         }catch(InterruptedException e) {//...}
      }
      goOn();
   }
}

或者,你可以等待使用<密码>的任意时间。

如果情况发生变化,或者在等待时间过后一段时间,情况会再次 wake,在这种情况下,情况会重新评价,如果情况没有改变,可能再次进入等候国。

public void run() {
   synchronized(mutex){
     if(condition){
         mutex.notify();
     }
   }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签