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

  • Home
  • SEARCH
  • 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 3781242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:55:04+00:00 2026-05-19T10:55:04+00:00

I use Thread in Java. I create a class that extend Thread and it’s

  • 0

I use Thread in Java. I create a class that extend Thread and it’s fine, but the problem is, when call this Thread, I don’t know the number of instances of this class, since the user must enter this number. For example:

multiThread multiThreadInstance = new multiThread(/* number entered from user */);
multiThreadInstance.start();  

This will call this thread once, but if i write:

multiThread multiThreadInstance1 = new multiThread(/* number entered from user */)
multiThreadInstance1.start()   
multiThread multiThreadInstance2 = new multiThread(/* number entered from user */)
multiThreadInstance2.start()  

This will call it twice at the same time, and so on.

If I put it in a for loop, then if user enters 3 for example, then start1 runs, when start1 finishes, start2 runs, when start2 finishes, start3 runs. I need to keep these instances of thread running in at the same time. How can i do this?

  • 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-19T10:55:05+00:00Added an answer on May 19, 2026 at 10:55 am

    You need to use java high level concurrency utilities to do this. Look at countdownlatches and executors. The following is a code that would do what you want. I recommend you read up on java concurrency utilities.

    import java.util.concurrent.*;
    
    public class ConcurrentTimer {
        private ConcurrentTimer() { } // Noninstantiable
    
        public static long time(Executor executor, int concurrency,
                final Runnable action) throws InterruptedException {
            final CountDownLatch ready = new CountDownLatch(concurrency);
            final CountDownLatch start = new CountDownLatch(1);
            final CountDownLatch done = new CountDownLatch(concurrency);
    
            for (int i = 0; i < concurrency; i++) {
                executor.execute(new Runnable() {
                    public void run() {
                        ready.countDown(); // Tell timer we're ready
                        try {
                            start.await(); // Wait till peers are ready
                            action.run();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        } finally {
                            done.countDown();  // Tell timer we're done
                        }
                    }
                });
            }
    
            ready.await();     // Wait for all workers to be ready
            long startNanos = System.nanoTime();
            start.countDown(); // And they're off!
            done.await();      // Wait for all workers to finish
            return System.nanoTime() - startNanos;
        }
    }
    

    A runnable version of the code example provided above: (Edited)

    import java.util.concurrent.*;
    
    public class ConcurrentTimer {
    
        public static void main(String[] args) {
    
    
            try {
                Runnable action = new Runnable() {
                        public void run() {
    
                            System.out.println("Thread Running");
    
                        }
                    };
    
                time (3, action);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    
        private ConcurrentTimer() { } // Noninstantiable
    
        public static long time(int concurrency,
                final Runnable action) throws InterruptedException {
    
            Executor executor = Executors.newFixedThreadPool(concurrency);
    
    
            final CountDownLatch ready = new CountDownLatch(concurrency);
            final CountDownLatch start = new CountDownLatch(1);
            final CountDownLatch done = new CountDownLatch(concurrency);
    
            for (int i = 0; i < concurrency; i++) {
                executor.execute(new Runnable() {
                    public void run() {
                        ready.countDown(); // Tell timer we're ready
                        try {
                            start.await(); // Wait till peers are ready
                            action.run();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        } finally {
                            done.countDown();  // Tell timer we're done
                        }
                    }
                });
            }
    
            ready.await();     // Wait for all workers to be ready
            long startNanos = System.nanoTime();
            start.countDown(); // And they're off!
            done.await();      // Wait for all workers to finish
            return System.nanoTime() - startNanos;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to use boost::thread in my program, but get the following compiler error
I use the Action<object>.BeginInvoke() method, does this use the thread pool or not? I
I want to use Thread.VolatileWrite() (or an equivalent function) to change the value of
Is it possible to use BackGroundWorker thread in ASP.NET 2.0 for the following scenario,
I'm trying to use a Thread in a simple winform. I have a ListBox
Playing with log4net, I have seen the possibility to use a per-thread stack of
I use C#'s BackgroundWorker object frequently to start a thread and perform a task.
How to decide whether to use threads or create separate process altogether in your
In Delphi 2009 I'm finding that any time I use TThread.CurrentThread in an application,
General tutorial or good resource on how to use threads in Python? When to

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.