ExecutorAdapter.java revision dc0051b2828ddc734f076f959a1d9d54c322654d
1package org.testng.internal.thread;
2
3
4import java.util.List;
5import java.util.concurrent.LinkedBlockingQueue;
6import java.util.concurrent.ThreadFactory;
7import java.util.concurrent.ThreadPoolExecutor;
8import java.util.concurrent.TimeUnit;
9
10/**
11 * An implementation for <code>IExecutor</code> based on <code>ThreadPoolExecutor</code>
12 *
13 * @author <a href="mailto:the_mindstorm@evolva.ro>Alexandru Popescu</a>
14 */
15public class ExecutorAdapter extends ThreadPoolExecutor implements IExecutor {
16  private IThreadFactory m_threadFactory;
17
18  public ExecutorAdapter(int threadCount, IThreadFactory tf) {
19      super(threadCount,
20            threadCount,
21            0L,
22            TimeUnit.MILLISECONDS,
23            new LinkedBlockingQueue<Runnable>(),
24            (ThreadFactory) tf.getThreadFactory());
25      m_threadFactory = tf;
26   }
27
28   public IFutureResult submitRunnable(final Runnable runnable) {
29      return new FutureResultAdapter(super.submit(runnable));
30   }
31
32   public void stopNow() {
33      super.shutdownNow();
34   }
35
36   public boolean awaitTermination(long timeout) {
37     boolean result= false;
38     try {
39      result= super.awaitTermination(timeout, TimeUnit.MILLISECONDS);
40     }
41     catch(InterruptedException iex) {
42       System.out.println("[WARN] ThreadPoolExecutor has been interrupted while awaiting termination");
43       Thread.currentThread().interrupt();
44     }
45
46     return result;
47   }
48
49  public StackTraceElement[][] getStackTraces() {
50    List<Thread> threads = m_threadFactory.getThreads();
51    int threadCount = threads.size();
52    StackTraceElement[][] result = new StackTraceElement[threadCount][];
53    for (int i = 0; i < result.length; i++) {
54      result[i] = threads.get(i).getStackTrace();
55    }
56    return result;
57  }
58}