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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:51:01+00:00 2026-06-08T10:51:01+00:00

So I have a method that starts five threads. I want to write a

  • 0

So I have a method that starts five threads. I want to write a unit test just to check that the five threads have been started. How do I do that? Sample codes are much appreciated.

  • 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-08T10:51:04+00:00Added an answer on June 8, 2026 at 10:51 am

    Instead of writing your own method to start threads, why not use an Executor, which can be injected into your class? Then you can easily test it by passing in a dummy Executor.

    Edit: Here’s a simple example of how your code could be structured:

    public class ResultCalculator {
        private final ExecutorService pool;
        private final List<Future<Integer>> pendingResults;
    
        public ResultCalculator(ExecutorService pool) {
            this.pool = pool;
            this.pendingResults = new ArrayList<Future<Integer>>();
        }
    
        public void startComputation() {
            for (int i = 0; i < 5; i++) {
                Future<Integer> future = pool.submit(new Robot(i));
                pendingResults.add(future);
            }
        }
    
        public int getFinalResult() throws ExecutionException {
            int total = 0;
            for (Future<Integer> robotResult : pendingResults) {
                total += robotResult.get();
            }
            return total;
        }
    }
    
    public class Robot implements Callable<Integer> {
        private final int input;
    
        public Robot(int input) {
            this.input = input;
        }
    
        @Override
        public Integer call() {
            // Some very long calculation
            Thread.sleep(10000);
    
            return input * input;
        }
    }
    

    And here’s how you’d call it from your main():

    public static void main(String args) throws Exception {
        // Note that the number of threads is now specified here
        ExecutorService pool = Executors.newFixedThreadPool(5);
        ResultCalculator calc = new ResultCalculator(pool);
        try {
            calc.startComputation();
            // Maybe do something while we're waiting
            System.out.printf("Result is: %d\n", calc.getFinalResult());
        } finally {
            pool.shutdownNow();
        }
    }
    

    And here’s how you’d test it (assuming JUnit 4 and Mockito):

    @Test
    @SuppressWarnings("unchecked")
    public void testStartComputationAddsRobotsToQueue() {
        ExecutorService pool = mock(ExecutorService.class);
        Future<Integer> future = mock(Future.class);
        when(pool.submit(any(Callable.class)).thenReturn(future);
    
        ResultCalculator calc = new ResultCalculator(pool);
        calc.startComputation();
    
        verify(pool, times(5)).submit(any(Callable.class));
    }
    

    Note that all this code is just a sketch which I have not tested or even tried to compile yet. But it should give you an idea of how the code can be structured.

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

Sidebar

Related Questions

I have one main thread that starts 10 other threads. I want that the
So I'm seeing fun stuff playing with threads. I have a method that starts
I have a method that prints private variable of the class. public class Test
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
I have a method that I need to run when the activity starts. I've
I have some method that is invoked on Application_Start. And it starts on first
I'm using django 1.0 and have a method in views.py that starts out like
I have a method that starts my game (startGame). I call it in viewDidLoad
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject

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.