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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:56:07+00:00 2026-06-14T21:56:07+00:00

I have decided to face this monster called concurrency and broaden my threading knowledge,

  • 0

I have decided to face this monster called concurrency and broaden my threading knowledge, so before I read through Joshua Bloch’s book, I decided to code something random to help me understand the problem I might face before reading the book, hopefully I can then come to my code and make corrections but then I landed into this pit and I am hoping someone can explain.

I have the following:

public class Arithmetic implements Runnable {
    int number;    
    public  Arithmetic(int x){      
      this.number = number + x;
    }
    @Override
    public void run() {         
        Thread.currentThread().setName("thread-"+number +" > " + "number = "+getNumber());
        System.out.println(Thread.currentThread().getName());        
    }    
        public  int getNumber() {
        return number;
    }
    public  void setNumber(int number) {
        this.number = getNumber() + number;
    }
}

Then a main class:

public class Main {  
    public static void main(String[] args) {    
      System.out.println(Thread.currentThread().getName());     
      for (int x=0; x<=5; x++){    
           Thread thread = new Thread(new Arithmetic(x));           
           thread.start();       
      }
    }
}

With this I get the following as output:

run:
main
thread-0 > number = 0
thread-1 > number = 1
thread-2 > number = 2
thread-3 > number = 3
thread-5 > number = 5
thread-4 > number = 4

Notice: The 5 comes before 4

But then I change my main class to:

public class Main {  
    public static void main(String[] args) {    
      System.out.println(Thread.currentThread().getName());     
      for (int x=0; x<=5; x++){    
           Runnable runnable  = new Arithmetic(x);           
          runnable.run();      
      }
    }
}

I get the output below:

run:
main
thread-0 > number = 0
thread-1 > number = 1
thread-2 > number = 2
thread-3 > number = 3
thread-4 > number = 4
thread-5 > number = 5

Notice: Correct sequence

I was hoping both main(s) will produce erratic result(Like Thread implementation), then I would then use some thread safety measures like synchronised access etc. but why does the Runnable call act as if Arithmetic is thread safe?

IMO, the difference between extending Thread class and implementing Runnable is for decoupling purposes. Apologies if this is a duplicate question, I can’t seem to find an answer.

Thanks in advance.

  • 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-14T21:56:08+00:00Added an answer on June 14, 2026 at 9:56 pm

    Runnables do not start new threads it is simply and interface, so this means your second piece of code runs synchronously.

    Threads are new threads so you they can run in parallel, which is why the output comes out of order – the 4th thread executes slower than the 5th.

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

Sidebar

Related Questions

Now before you face palm and click on duplicate entry or the like, read
I have looked around online and decided to use the following @font-face implementation: My
I have decided to add some validation to my combobox, what I'm trying to
I have decided to start using unit testing in PL/SQL, since it could be
I have decided to develop a (Unicode) spell checker for my final year project
We have decided to work with the Insert Framework Here We'll most likely update
Now that I have decided upon Firebird , with the help of StackOverflow :),
I have a class that has an inner struct/class (I have decided which yet,
I have started a new web application and have decided to use jquery as
I am new to Silverlight and have decided to give it a go. I

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.