• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitoutil;
6 
7 import java.util.LinkedList;
8 import java.util.List;
9 
10 /**
11  * Utility methods for concurrent testing
12  */
13 public class ConcurrentTesting {
14 
15     /**
16      * Executes given runnable in thread and waits for completion
17      */
inThread(Runnable r)18     public static void inThread(Runnable r) throws InterruptedException {
19         Thread t = new Thread(r);
20         t.start();
21         t.join();
22     }
23 
24     /**
25      * Starts all supplied runnables and then waits for all of them to complete.
26      * Runnables are executed concurrently.
27      */
concurrently(Runnable .... runnables)28     public static void concurrently(Runnable ... runnables) throws InterruptedException {
29         List<Thread> threads = new LinkedList<Thread>();
30         for (Runnable r : runnables) {
31             Thread t = new Thread(r);
32             t.start();
33             threads.add(t);
34         }
35 
36         for (Thread t : threads) {
37             t.join();
38         }
39     }
40 }
41