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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:12:42+00:00 2026-05-14T02:12:42+00:00

Could anyone give an example program that explains Java Threads in a simple way?

  • 0

Could anyone give an example program that explains Java Threads in a simple way? For example, say I have three threads t1, t2 and t3. I want a code that demonstrates that the threads execute simultaneously, and not sequentially.

  • 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-14T02:12:42+00:00Added an answer on May 14, 2026 at 2:12 am

    Here is a simple example:

    ThreadTest.java

    public class ThreadTest
    {
       public static void main(String [] args)
       {
          MyThread t1 = new MyThread(0, 3, 300);
          MyThread t2 = new MyThread(1, 3, 300);
          MyThread t3 = new MyThread(2, 3, 300);
    
          t1.start();
          t2.start();
          t3.start();
       }
    }
    

    MyThread.java

    public class MyThread extends Thread
    {
       private int startIdx, nThreads, maxIdx;
    
       public MyThread(int s, int n, int m)
       {
          this.startIdx = s;
          this.nThreads = n;
          this.maxIdx = m;
       }
    
       @Override
       public void run()
       {
          for(int i = this.startIdx; i < this.maxIdx; i += this.nThreads)
          {
             System.out.println("[ID " + this.getId() + "] " + i);
          }
       }
    }
    

    And some output:

    [ID 9] 1
    [ID 10] 2
    [ID 8] 0
    [ID 10] 5
    [ID 9] 4
    [ID 10] 8
    [ID 8] 3
    [ID 10] 11
    [ID 10] 14
    [ID 10] 17
    [ID 10] 20
    [ID 10] 23
    

    An explanation – Each MyThread object tries to print numbers from 0 to 300, but they are only responsible for certain regions of that range. I chose to split it by indices, with each thread jumping ahead by the number of threads total. So t1 does index 0, 3, 6, 9, etc.

    Now, without IO, trivial calculations like this can still look like threads are executing sequentially, which is why I just showed the first part of the output. On my computer, after this output thread with ID 10 finishes all at once, followed by 9, then 8. If you put in a wait or a yield, you can see it better:

    MyThread.java

    System.out.println("[ID " + this.getId() + "] " + i);
    Thread.yield();
    

    And the output:

    [ID 8] 0
    [ID 9] 1
    [ID 10] 2
    [ID 8] 3
    [ID 9] 4
    [ID 8] 6
    [ID 10] 5
    [ID 9] 7
    

    Now you can see each thread executing, giving up control early, and the next executing.

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

Sidebar

Related Questions

Say i have an array $array Could anyone give me an example of how
Hi Could anyone give me a sample program to Create an ApplyRemoveConst template that
Could anyone give me a small and simple example of how to do this?
Could anyone please give me an example in this? I have found many but
Could anyone give a simple example of an Oracle stored procedure for updating two
Dear Stackoverflow user, Could anyone give me an simple example of restful webservice with
Could anyone give me example code to access the command line through VBScript? I
Could anyone give me an example about how to do fuzzy matching of two
I was wondering if anyone could give me an example or point me to
Could anyone please give a sample or any link that describes how to spawn

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.