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 8039317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:32:01+00:00 2026-06-05T03:32:01+00:00

Please show me how to make thread wait. for example wait if i ==

  • 0

Please show me how to make thread wait. for example wait if i == 0 and go again when i == 1

public class Main {

    public Main() { 
    }

    public void method() {

            Thread thread = new Thread(new Task());
            // I want to make wait it when I want
            // for example wait if i == 0 and go again when i = 1
    }

    public static void main(String[] args) {
            new Main();
    }
}
  • 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-05T03:32:02+00:00Added an answer on June 5, 2026 at 3:32 am

    To avoid active waiting try use wait() and notify() or notifyAll() methods. Wait() can make thread stop until someone call notify() or notifyAll() on same object as wait(). One of condition is that thread must be in possession of monitor of object on which will be invoking wait(), notify() or notifyAll().

    Here is an example

    import java.util.concurrent.TimeUnit;
    
    public class StartPauseDemo extends Thread {
        volatile int i = 1;
    
        public void pause() {
            i = 0;
        }
    
        public synchronized void unPause() {
            i = 1;
            notify();// wake up thread
        }
    
        @Override
        public void run() {
            while (i==1) {
                // logic of method for example printing time every 200 miliseconds
                System.out.println(System.currentTimeMillis());
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                if (i==0) {
                    synchronized (this) {// thread must possess monitor of object on
                                            // which will be called wait() method,
                                            // in our case current thread object
                        try {
                            wait();// wait until someone calls notify() or notifyAll
                                    // on this thred object
                                    // (in our case it is done in unPause() method)
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
        // test - pausing and unpausing every 1 sec
        public static void main(String[] args) throws InterruptedException {
            StartPauseDemo sp = new StartPauseDemo();
            sp.start();// start thread
            while (true) {
                System.out.println("pausing");
                sp.pause();
                TimeUnit.SECONDS.sleep(1);
    
                System.out.println("unpausing");
                sp.unPause();
                TimeUnit.SECONDS.sleep(1);
            }
        }
    }
    

    Output:

    pausing
    unpausing
    1338726153307
    1338726153507
    1338726153709
    1338726153909
    1338726154109
    pausing
    unpausing
    1338726155307
    1338726155507
    ... and so on
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to show text please wait while app is searching for satellite
please i am trying to make the message show on newline as the customer
Can someone please show me a code example about how to get a long
can someone please show me documentation on this method? i have the following line:
Given the example below, can someone please show me how this could be called?
guys please post me an example for threading in MFC....it should show the progress
Please show me how to make post of big text data with curllib? I
could you please show me the solution to make the input text of password
Here is a few bit of code: public class ShowDialog extends Thread { private
Will someone PLEASE show me what I'm doing wrong? I'm new to using phpMyAdmin

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.