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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:23:47+00:00 2026-05-31T11:23:47+00:00

In this multithreading program, when I run it, I always get the output in

  • 0

In this multithreading program, when I run it, I always get the output in some random order. But I was wondering if there is any way I can make this program to work in synchronized mode. Like when I runt it then for the first thread it should print out everything, then for second thread it should print out something, then for third thread it should print out everything etc etc. So sample output should be like this for each thread-

Task 1 Started
original: Hello World
Difference:- 0
Task 1 Ended

Task 2 Started
original: Hello World
Difference:- 0
Task 2 Ended

............
............


Task 15 Started
original: Hello World
Difference:- 0
Task 15 Ended

This is my below program. Any suggestions will be appreciated.

class ThreadTask implements Runnable {
    private int id;

    public ThreadTask(int id) {
        this.id = id;
    }

    public synchronized void run() {

        System.out.println("Task " + id + " Started ");

        String originalString = "Hello World";

        System.out.println("original: " + originalString);

        System.out.println("Task " + id + " Ended ");

    }
}

public class TestPool {

    public static void main(String[] args) throws InterruptedException {
        int size = 5; //Integer.parseInt(args[0]);

        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(size); 

        // queue some tasks
        for(int i = 1; i <= 3 * size; i++) {
            service.submit(new ThreadTask(i));
        }


        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }
}
  • 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-31T11:23:49+00:00Added an answer on May 31, 2026 at 11:23 am

    You commented on Jakub’s answer as follows:

    Can you give me example basis on my code as I just started learning about threading. It will be of great help to me.

    What Jakub is saying is that forcing threads to run in a fixed sequence defeats the purpose of using threads in the first place. Think about this.


    If you really do want / need your example to run the tasks in order, you may as well do this:

    for (int i = 1; i <= 3 * size; i++) {
            new ThreadTask(i).run();
    }
    

    i.e. just run the runnables in the current thread.

    Or you could set the maximum pool size to 1, which forces the service to run the tasks in order. (Of course, this defeats the point of using threads. You won’t get any parallelism this way.)


    A more sensible approach would be to have each thread return its results in a Future, and then have the main thread fetch the value from each future (in the required order) and print it. Basically, you want to allow the threads to run in any order (and in parallel, if you have multiple cores), but then impose the ordering when you access the results.

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

Sidebar

Related Questions

I am using multi threading concept to run some process. this process uses the
I am considering using OpenMP for multithreading in C++. But I wonder if there
I am new to Java Multithreading World, I wrote this program, I just wanted
I'm working on a server program, which uses multithreading. The problem is, there are
I'm new to Java, and I need some help working on this program. This
This somehow simple task is not so simple. I can get the number of
This should be simple, but the answer is eluding me. If I've got a
this is my code that I write it but I want to use LINQ
This is probably a stupid question, but how much of a security risk is
This is a complex question, because there are a lot of moving parts. My

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.