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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:37:35+00:00 2026-05-25T11:37:35+00:00

I am trying to understand below program. If I call new ReaderThread().start() it is

  • 0

I am trying to understand below program. If I call new ReaderThread().start() it is working fine, but if I call new ReaderThread().run(), the application goes into an infinite loop. What is the difference?

public class Contention {

    private static boolean ready;
    private static int number;

    private static class ReaderThread extends Thread {
        public void run() {
                 while (!ready){
                System.out.println("ready ..."+ready);
                Thread.yield();}
            System.out.println(number);
           // }
        }
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new ReaderThread().run();
        number = 42;
        ready = true;

    }

}
  • 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-25T11:37:36+00:00Added an answer on May 25, 2026 at 11:37 am

    If you use new ReaderThread().start();,you are creating actually a new thread instance which will run in the background and main() resumes with its further execution.

    But new ReaderThread().run(); creates an instance of this class and makes a normal method call to the run() method, so the main() has to wait till the run() has finished executing and returned the control back to main(), which in your case is an infinite loop.

    If you want to start a new thread then start by using ReaderThread().start(); this is the correct way to start a thread, there are no alternatives for this.

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

Sidebar

Related Questions

Details below but basically I'm trying to understand what on my Droid is throwing
I am trying to solve the question below, but I do not understand it
I'm trying to understand why the second example below works with no issues, but
I am trying to understand the output of the program printed below. When I
I grabbed this code form JCarousel and just trying to understand these lines below.
I am trying to understand what is happening in the getter below, this is
I've been learning C#, and I'm trying to understand lambdas. In this sample below,
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
I am trying to understand http://beej.us/guide/bgnet/examples/select.c (included below for reference). I am doing this:

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.