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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:35:10+00:00 2026-05-31T13:35:10+00:00

I have the following code that runs whenever you click the Start button on

  • 0

I have the following code that runs whenever you click the Start button on my program. I have denoted via comments where I want the timer to go, problem is, when I do thread.sleep(time) it freezes my program! So, I was wondering if someoen could just simply add atimer to my code so it runs the first bit, waits, then runs it again based on bumpNum.

Code:

public class startReplyButtonListener implements ActionListener{


        public void actionPerformed(ActionEvent ev){

            int length = textAreaReplyMessage.getText().length();
            int remLen = 400 - length;

            String strHTML = neo.get("http://www.neopets.com/neoboards/topic.phtml?topic=" + txtTopicID.getText());
            /*strHTML = neo.post("/neoboards/process_topic.phtml?", new String[][] {{"boardType", "topic_id", "board_id", "message", "next", "remLen"}, {"reply", txtTopicID.getText(), "4", textAreaReplyMessage.getText() , "1", ((Integer)remLen).toString()}});

            if(strHTML.contains("No topic with ID")){
                txtLog.append("Invalid Topic ID! \n");
            }
            else{
                txtLog.append("Bumped Topic ID " + txtTopicID.getText() + "\n");
            }
            */
            System.out.println(strHTML);
            bumpNum = 5;
            wait = Integer.parseInt(textWait1.getText()) * 1000; //converting to miliseconds

            int i=1;
            do{
                strHTML = neo.post("/neoboards/process_topic.phtml?", new String[][] {{"boardType", "topic_id", "board_id", "message", "next", "remLen"}, {"reply", txtTopicID.getText(), "4", textAreaReplyMessage.getText() , "1", ((Integer)remLen).toString()}});

                txtLog.append("Board Bumped. Waiting "+ ((Integer)(wait/1000)).toString() +" Seconds..." + "\n");

                //ADD TIMER HERE
                i++;



            }while(i <= bumpNum);

        }

        }

What I wish to accomplish:

User indicates how many times they want to “post”(indicated by bumpNum), the loop will first, post once:

strHTML = neo.post("/neoboards/process_topic.phtml?", new String[][] {{"boardType", "topic_id", "board_id", "message", "next", "remLen"}, {"reply", txtTopicID.getText(), "4", textAreaReplyMessage.getText() , "1", ((Integer)remLen).toString()}});

Then:
Based on users input, it will wait for however many seconds(txtWait1) and THEN repeat the posting code above until it has reached bumpNum.

And it will update txtLog with the following EACH TIME it bumps(so the program cannot be frozen):

txtLog.append("Board Bumped. Waiting "+ ((Integer)(wait/1000)).toString() +" Seconds..." + "\n");
  • 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-31T13:35:11+00:00Added an answer on May 31, 2026 at 1:35 pm

    Edit:

    Sigh. Ok, now I understand. I don’t know the answer. You are talking about drawing a GUI element. I suspect you want to fork a thread to do a job and then show the GUI display that you are waiting for it. You need to wait for the thread to finish (see my join code below) all of the time having the GUI element refresh UNTIL it finishes when you display some result.

    This depends more on the GUI code than sleep/timer. I would start a new question now and explain !!!NOT WITH CODE!!! but with pseudo code from 1000 foot view what you want. Something like:

    I am trying to fork a thread that runs in the background in [Swing/Android/etc]. I want to display to the user that the thread has been forked, I want the user interface to wait for the thread without freezing, and then I want the user interface to join with the thread and display the results.

    Think about the problem like we have to think of it. Anticipate questions that we will ask. Figure out what we don’t and can’t know about your environment.

    Best of luck.

    Edit:

    If you are just trying to call sleep then you don’t need to fork a thread for that. All you need to do in your code is:

      try {
          Thread.sleep(waitingTime);
          System.out.println(waitingTime);
      } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          e.printStackTrace();
      }
    

    This will pause the current thread (which could be the main thread) for waitingTime milliseconds.


    So you are forking 3 threads very quickly which I guess you don’t want to do. If you are trying to wait for each thread to finish then you will have to do something like:

     Thread thread = new Thread(new Counter(wait));
     thread.start();
     thread.join();
    

    Couple of other comments:

    • It is considered bad form to start a thread in the constructor of a class: new Thread(this).start();
    • You are creating 2 thread objects inside of your Runnable. You should just create one outside of your Runnable. See above.

      Thread myCounter = new Thread(this);  << #1
      public Counter(int waitingTime) {
         new Thread(this).start();          << #2
      }
      
    • I would not initialize waitingTime = 0; when defined and initialize it in the constructor. This is confusing. Remove the = 0.

          int waitingTime;                    << remove the =0 here
          public Counter(int waitingTime) {
             this.waitingTime = waitingTime;
      
    • When you catch InterruptedException, be sure to handle it right. A good pattern is to reset the interrupt flag and/or quit the thread:

      } catch (InterruptedException e) {
          // resets the interrupt flag cleared by catching the exception
          Thread.currentThread.interrupt();
          // or stops the thread immediately
          return;
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL code that runs against a Change Request database. Each
I want to have code that runs as efficiently as possible. I have views
I have a web application that does the following: You click a button to
I have the following php code that runs after validation: try { if (isset($filtered)
I have an Activity that runs the following code (time and interval are defined):
I have the following code that runs figlet that has input as a range.
Imagine I have the following code that runs as a background processor for an
I have following code that does not work due to a being a value
I have following code snippet that i use to compile class at the run
I have the following code that shows either a bug or a misunderstanding on

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.