Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:38:46+00:00 2026-05-26T03:38:46+00:00

I have a loop which need to be turned off so that it can

  • 0

I have a loop which need to be turned off so that it can continue to next statements with some values else it crash. What is the best way to do so? For example:

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); 
   ...
}

Is this the best way to handle the realtime while loop, so that i can exit exactly after 20 times limit over to the next.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:38:47+00:00Added an answer on May 26, 2026 at 3:38 am
    for (int i = 0; main.strings != null && i <= 20; ++i) {
      Thread.sleep(1000);
    }
    

    will do it, but I would really look into using main.wait(20000) and have the thing that sets main.strings do main.notifyAll() afterwards.

    See http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html for a good tutorial.

    Threads often have to coordinate their actions. The most common coordination idiom is the guarded block. Such a block begins by polling a condition that must be true before the block can proceed. There are a number of steps to follow in order to do this correctly.

    Suppose, for example guardedJoy is a method that must not proceed until a shared variable joy has been set by another thread. Such a method could, in theory, simply loop until the condition is satisfied, but that loop is wasteful, since it executes continuously while waiting.

    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!");
    }
    

    …

    When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened:

    public synchronized notifyJoy() {
        joy = true;
        notifyAll();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a for loop which is nested within another, how can I
I have some code which collects points (consed integers) from a loop which looks
I have two SQL scripts which get called within a loop that accept a
i have a input tag which is non editable, but some times i need
I have the following command which will loop over all the subdirectories in a
In Java 5 and above you have the foreach loop, which works magically on
I have a loop that reads each line in a file using getline() :
I have a loop on page to update an access database that takes 15-20
In my WPF client, I have a loop that calls a WCF service to
I have a main loop which is fully data-driven: it has a blocking call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.