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