1 22 package org.jboss.test.tm.test; 23 24 import java.util.ArrayList ; 25 26 import org.jboss.test.JBossTestCase; 27 28 34 public class AbstractConcurrentStressTest extends JBossTestCase 35 { 36 private ArrayList done = new ArrayList (); 37 private int total; 38 39 public interface ConcurrentTestCallback 40 { 41 void finished() throws Throwable ; 42 } 43 44 public void runConcurrentTest(ConcurrentRunnable[] runnables, ConcurrentTestCallback callback) throws Throwable 45 { 46 total = runnables.length; 47 Thread [] threads = new Thread [total]; 48 for (int i = 0; i < total; ++i) 49 { 50 threads[i] = new Thread (runnables[i], getName() + "-" + i); 51 threads[i].start(); 52 } 53 for (int i = 0; i < total; ++i) 54 threads[i].join(); 55 56 if (callback != null) 57 callback.finished(); 58 59 for (int i = 0; i < total; ++i) 60 runnables[i].doCheck(); 61 } 62 63 public abstract class ConcurrentRunnable implements Runnable 64 { 65 public Throwable failure; 66 67 public abstract void doStart(); 68 public abstract void doRun(); 69 public abstract void doEnd(); 70 public void doCheck() throws Throwable 71 { 72 if (failure != null) 73 throw failure; 74 } 75 76 public void run() 77 { 78 doStart(); 79 waitDone(); 80 for (int i =0; i < getIterationCount(); ++i) 81 doRun(); 82 waitDone(); 83 doEnd(); 84 waitDone(); 85 } 86 } 87 88 protected synchronized void waitDone() 89 { 90 if (done.size() < total - 1) 91 { 92 done.add(this); 93 doWait(); 94 } 95 else 96 { 97 for (int i = 0; i < done.size(); ++i) 98 done.get(i).notify(); 99 done.clear(); 100 } 101 } 102 103 protected void doWait() 104 { 105 boolean interrupted = false; 106 try 107 { 108 while (true) 109 { 110 try 111 { 112 wait(); 113 return; 114 } 115 catch (InterruptedException e) 116 { 117 interrupted = true; 118 } 119 } 120 } 121 finally 122 { 123 if (interrupted) 124 Thread.currentThread().interrupt(); 125 } 126 127 } 128 129 public AbstractConcurrentStressTest(String name) 130 { 131 super(name); 132 } 133 } 134 | Popular Tags |