English 中文(简体)
怎样才能从一生中退出? 根据我预先确定的在 Java的计数
原标题:How can i exit from a while loop? Based on my pre-defined count down in Java

我有一席之地,需要放弃,以便它能够继续发表带有其他价值的发言。 这样做的最佳方式是什么? 例如:

if (data.startsWith("tcp") ) 
{
   /* main.strings should have a value but it may take a while */
   main.start("i generate some encoding/decoding, i take uncertain time...");

   int i = 0;
   while(main.strings != null) 
   {
     /* Maximum 20 time waiting is accepted, else not */
     if (i > 20 ) break; 
     Thread.sleep(1000);
     i++;
   }

   /* It crash, if main.start() is not creating the main.strings  
    * so there is a uncertain specific time, for this but we however can t wait long
    */
   main.NowUDPSend(main.strings); 
   ...
}

这是在休息期间处理实时问题的最佳途径,这样一来,在20倍的时间里,我就能够离开。

最佳回答
for (int i = 0; main.strings != null && i <= 20; ++i) {
  Thread.sleep(1000);
}

将使用main.wait (0), , 并且有下列内容:main.strings ,docs/api/java/article/contentr=#28%。

。 a. 接受良好教学。

往往必须协调它们的行动。 最常见的协调部分是守卫区。 投票开始后,投票必须满足条件,才能进入投票区。 为了正确做到这一点,采取了一些步骤。

例如,<代码>受保障监督的Joy是一种方法,在另一条路面确定共同可变的喜悦之前不得继续使用。 从理论上讲,这种方法只能停留在满足条件之前,但这种做法是浪费的,因为它在等待时不断执行。

public void guardedJoy() {
  //Simple loop guard. Wastes processor time. Don t do this!
  while(!joy) {}
  System.out.println("Joy has been achieved!");
}

A more efficient guard invokes Object.wait to suspend the current thread. The invocation of wait does not return until another thread has issued a notification that some special event may have occurred — though not necessarily the event this thread is waiting for:

public synchronized guardedJoy() {
    //This guard only loops once for each special event, which may not
    //be the event we re waiting for.
    while(!joy) {
        try {
            wait();
        } catch (InterruptedException e) {}
    }
    System.out.println("Joy and efficiency have been achieved!");
}

......

当援引<条码>wait时,便可24小时释放并暂停执行。 在今后某个时候,另一条之行将获得同一锁,并援引<条码>。 All , informing all threadsimmer at that lock that some important has dated:

public synchronized notifyJoy() {
    joy = true;
    notifyAll();
}
问题回答




相关问题
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 ...

热门标签