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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:12:36+00:00 2026-05-13T10:12:36+00:00

I have something like this: for(int i=0; i<5; i++){ mytextarea.setText(hello + i); try{ Thread.currentThread().sleep(1000);

  • 0

I have something like this:

for(int i=0; i<5; i++){
    mytextarea.setText("hello " + i);
    try{
        Thread.currentThread().sleep(1000); //to give time for users to read
    } catch(Exception e){}
}

I’m expecting it will display “hello 0” in the text area, wait for 1 second, then display “hello 1”, then wait for 1 second, etc.

But what happen is different, it waits for 5 seconds, then it displays “hello 4”.

Any idea?

  • 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-13T10:12:36+00:00Added an answer on May 13, 2026 at 10:12 am

    Yes – you’re basically blocking the UI thread, so it’s never getting round to actually updating.

    Sleeping in a UI thread is a really bad idea.

    If you want to do something like this, you should use a Timer. (I’m assuming you’re using Swing. If not, please edit your question to indicate which UI framework you’re using.)

    You should also note that Thread.sleep is a static method. You’re using it as if it were an instance method. Admittedly you happen to be calling it “on” the current thread, but your usage suggest that you think that:

    Thread t = new Thread(...);
    t.start();
    t.sleep(1000);
    

    would make the new thread sleep. It wouldn’t – it would make the current thread sleep, because that’s what Thread.sleep always does. IMO it was a mistake for Java to allow you to call static methods in this way – if you’re using Eclipse, there’s an option to make this a warning or error.

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

Sidebar

Related Questions

I have something like this: (pseudocode) public class Author { int id; public List<Thread>
I have something like this that is incorect: vector<boost::thread> vec; for(int agent = 1;
I have something like this: create table account ( id int identity(1,1) primary key,
Suppose I have something like this: class A { public B mem; public int
Okay, I have something like this in C++: class MyClass{ private: int someVariable; int
I have a constexpr function that looks something like this: constexpr int foo(int bar)
I have two tables which looks something like this Table Queue int ID; string
I have class Employee which is something like this. class Emp { int EmpID;
Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int
Suppose I have something like this int strLen; printf(Please enter a number: ); scanf(%d,

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.