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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:08:45+00:00 2026-06-14T08:08:45+00:00

I have the following code: public class ThreadTest implements Runnable { public int ThrCount

  • 0

I have the following code:

public class ThreadTest implements Runnable {
    public int ThrCount    = 0;

    public void ThrCountIncr() {
        while (true) {
            ThrCount++;
            System.out.println(ThrCount);
            try {
                Thread.currentThread().sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void run() {
        while (true) {
            if (ThrCount > 10) {
                System.out.println(ThrCount + "\n Thread finished");
                System.exit(1);
            }
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

But when I remove this line from run, it stops working:

Thread.currentThread().sleep(100);

First, I start the thread, then I use ThrCountIncr.

ThreadTest Thrtest = new ThreadTest();
Thread thr = new Thread(Thrtest);
thr.start();
Thrtest.ThrCountIncr();

The thread checks the ThrCount variable value, and if it is greater than 10, it stops the program. Without sleep(100), the thread doesn’t stop the program, and I think it doesn’t check the variable value. Why does the call to sleep make this code work?

  • 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-14T08:08:46+00:00Added an answer on June 14, 2026 at 8:08 am

    Even with the Thread.sleep() it might not work. This is because you don’t properly synchronize accesses to the shared ThrCount variable.

    If you make that variable volatile, you should not see any issues any longer. However it might not loop exactly 10 times as the ++ operation is not atomic.

    Ideally, you should use an AtomicInteger and use its incrementAndGet() method.

    Also note:

    • Java naming conventions: variables and method names should start in lower case (thrCount, thrCountIncr()
    • sleep is a static method so you can simply call Thread.sleep(...); and it will sleep in the current thread.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public class MyLocationListener implements LocationListener { @Override public void
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public class Test extends JFrame implements ActionListener{ private static
If I have the following code: public class Foo { public void Bar() {
Let's assume I have the following code: public class MainClass { public static void
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called
I have the following code: public class Test { public static void Main() {
I have the following code: public class LSD{ public static int R=1<<8; public static
I have the following code : public class Main { private int i =
I have the following code: public class Tests { public static void main(String[] args)

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.