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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:27:17+00:00 2026-05-25T19:27:17+00:00

I have the following thread which simply prints a dot every 200ms: public class

  • 0

I have the following thread which simply prints a dot every 200ms:

public class Progress {

    private static boolean threadCanRun = true;
    private static Thread progressThread = new Thread(new Runnable() 
    {
        public void run() {
            while (threadCanRun) {
                System.out.print('.');
                System.out.flush();
                try {
                    progressThread.sleep(200);
                } catch (InterruptedException ex) {}
            }
        }
    });

    public static void stop()
    {
        threadCanRun = false;
        progressThread.interrupt();
    }

    public static void start()
    {
        if (!progressThread.isAlive())
        {
            progressThread.start();
        } else
        {
            threadCanRun = true;
        }
    }

}

I start the thread with this code (for now):

 System.out.println("Working.");
 Progress.start();


 try {
        Thread.sleep(10000); //To be replaced with code that does work.
 } catch (InterruptedException ex) {}

 Progress.stop();

What’s really strange is this:

If I use System.out.println('.'); , the code works exactly as expected. (Apart from the fact that I don’t want a new line each time).

With System.out.print('.');, the code waits for ten seconds, and then shows the output.

System.out.println:

     Print dot, wait 200ms, print dot, wait 200ms etc...

System.out.print:

     Wait 5000ms, Print all dots

What is happening, and what can I do to go around this behaviour?

EDIT:

I have also tried this:

private static synchronized void printDot()
{
    System.err.print('.');
}

and printDot() instead of System.out.print('.');
It still doesn’t work.

EDIT2:

Interesting. This code works as expected:

        System.out.print('.');
        System.out.flush();  //Makes no difference with or without
        System.out.println();

This doesn’t:

        System.err.print('.');
        System.err.flush();
        System.out.print('.');
        System.out.flush();

Solution: The issue was netbeans related. It worked fine when I run it as a jar file from java -jar.

This is one of the most frustrating errors I have seen in my life. When I try to run this code with breakpoints in debug mode, everything works correctly.

  • 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-25T19:27:17+00:00Added an answer on May 25, 2026 at 7:27 pm

    The stdout is line buffered.
    Use stderr, or flush the PrintStream after each print.

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

Sidebar

Related Questions

I have a static utility class, FileUtils which has the following method: public static
I have a DirectoryMonitor class which works on another thread. It has the following
Supposing I have the following public class Foo { private Map<Integer,SomeObject> myMap; public Foo()
I'm following the example given in http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/ I have a thread which is checking
I have the following error Exception in thread main javax.naming.NameNotFoundException: CounterBean not bound trying
I have the following code in my program: Thread getUsersist, getChatUsers; getUsersList = new
I have the following code in a Runnable that gets passed to a thread
Let's say that I have the following code that's run in one thread of
I have a thread that does the following: 1) Do some work 2) Wait
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency

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.