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

  • Home
  • SEARCH
  • 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 8699271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:54:32+00:00 2026-06-13T01:54:32+00:00

I have a function that needs to be called once a boolean variable is

  • 0

I have a function that needs to be called once a boolean variable is true. I tried using a while loop in a thread but it doesn’t work. Here is what I’ve tried:

public class MyRunnable implements Runnable {

public void run() {
    while (true) {
         if (conditions == true) { 
             System.out.println("second");
             break;
         }
    }
}

public static void main(String args[]) {
    boolean condition = false;
    (new Thread(new MyRunnable())).start();
    System.out.println("first\n");
    // set conndition to true
    condition = true;

    }

}

The result shoud be:

first
second
  • 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-06-13T01:54:33+00:00Added an answer on June 13, 2026 at 1:54 am

    Do not busy-wait for such conditions. Use a blocking idiom. For your simple case you would get away with a new CountDownLatch(1). First, here’s your code, but fixed to compile and run the way you expect:

    public class MyRunnable implements Runnable {
      volatile boolean condition = false;
    
      public void run() {
        while (true) {
          if (condition) {
            System.out.println("second");
            break;
          }
        }
      }
      public static void main(String args[]) {
        final MyRunnable r = new MyRunnable();
        new Thread(r).start();
        System.out.println("first\n");
        r.condition = true;
      }
    }
    

    For comparison, a program with a CountDownLatch:

    public class MyRunnable implements Runnable {
      final CountDownLatch latch = new CountDownLatch(1);
    
      public void run() {
        try { latch.await(); } catch (InterruptedException e) {}
        System.out.println("second");
      }
    
      public static void main(String args[]) {
        final MyRunnable r = new MyRunnable();
        new Thread(r).start();
        System.out.println("first\n");
        r.latch.countDown();
      }
    }
    

    To truly notice the difference, add a Thread.sleep(20000) after println("first") and hear the difference in the sound of your computer’s fan working hard to dissipate the energy the first program is wasting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a moderate size (40-odd function) C API that needs to be called
I have a function that needs to access a variable in its parent environment
I have a re-useable function in some CUDA code that needs to be called
I have a function that needs to call a virtual method many times in
I have a function that needs to append a sequence of numbers (starting with
I have a c# function that needs to write to a SQL image column.
I have a function that might fail, so the value it returns needs to
I have an if statement that needs to look like this: UPDATE $(input#textbox).keypress(function(e){ key==e.which;
I have a win form that creates a site in IIS7. One function needs
I have a function that i need to call on iframe mousemove(). But i

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.