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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:54:46+00:00 2026-06-14T13:54:46+00:00

If I have the following dummy code: public static void main(String[] args) { TestRunnable

  • 0

If I have the following dummy code:

public static void main(String[] args) {
  TestRunnable test1 = new TestRunnable();
  TestRunnable test2 = new TestRunnable();
  Thread thread1 = new Thread(test1);
  Thread thread2 = new Thread(test2);
  thread1.start();
  thread2.start();
}

public static class TestRunnable implements Runnable {
  @Override
  public void run() {
    while(true) {
      //bla bla
    }
  }
}

In my current program I have a similar structure i.e. two threads executing the same Run() method. But for some reason only thread 1 is given CPU time i.e. thread 2 never gets a chance to run. Is this because while thread 1 is in its while loop , thread 2 waits?

I’m not exactly sure, if a thread is in a while loop is it “blocking” other threads? I would think so, but not 100% sure so it would be nice to know if anyone could inform me of what actually is happening here.

EDIT
Okay, just tried to make a really simple example again and now both threads are getting CPU time. However this is not the case in my original program. Must be some bug somewhere. Looking into that now. Thanks to everyone for clearing it up, at least I got that knowledge.

  • 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-14T13:54:48+00:00Added an answer on June 14, 2026 at 1:54 pm

    There is no guarantee by the JVM that it will halt a busy thread to give other threads some CPU.

    It’s good practice to call Thread.yield();, or if that doesn’t work call Thread.sleep(100);, inside your busy loop to let other threads have some CPU.

    • 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: class SuperClass { public static String getName() { return
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the
I have the following scala code: package dummy import javax.servlet.http.{HttpServlet, HttpServletRequest => HSReq, HttpServletResponse
Say I have the following piece of java code ArrayList<Double> myList = new Double[100];
I have a small dummy project in VS 2008, contains only the following code
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
I have the following code: <?php require_once('IPTC.php'); $iptc = new Image_IPTC('001.jpg'); print_r($iptc); ?> And
I have the following setup code: MockOf<IObjectSet<Dummy>>().Setup(c => c.AddObject(dummy)).Verifiable(); MockOf<IObjectContextWrapper>().Setup(c => c.GetObjectSet<Dummy>()).Returns(MockOf<IObjectSet<Dummy>>().Object); where Dummy
I tried this dummy code below to test unnamed namespace. I have the following
I have the following simple test code at my server http handler: String cookieString

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.