public void run() throws Exception { Thread[] threads = new Thread[THREADS]; for (int i = 0; i < THREADS; i++) { try { threads[i] = new Thread(peerFactory.newClient(this), "Client " + i); } catch (Exception e) { e.printStackTrace(); return; } threads[i].start(); } try { for (int i = 0; i < THREADS; i++) { threads[i].join(); } } catch (InterruptedException e) { setFailed(); e.printStackTrace(); } if (failed) { throw new Exception("*** Test '" + peerFactory.getName() + "' failed ***"); } else { System.out.println("Test '" + peerFactory.getName() + "' completed successfully"); } }
private void waitUntilRunning() { synchronized (taskWorkerLock) { if (isExit) return; while (!isRunning) { try { taskWorkerLock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public void run() { if (DEBUG) System.err.println("MainThread.run(): " + Thread.currentThread().getName()); synchronized (taskWorkerLock) { isRunning = true; taskWorkerLock.notifyAll(); } while (!shouldStop) { try { // wait for something todo .. synchronized (taskWorkerLock) { while (!shouldStop && tasks.size() == 0) { try { taskWorkerLock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // take over the tasks .. if (!shouldStop && tasks.size() > 0) { Runnable task = (Runnable) tasks.remove(0); task.run(); // FIXME: could be run outside of lock } taskWorkerLock.notifyAll(); } } catch (Throwable t) { // handle errors .. t.printStackTrace(); } finally { // epilog - unlock locked stuff } } if (DEBUG) System.err.println("MainThread.run(): " + Thread.currentThread().getName() + " fin"); synchronized (taskWorkerLock) { isRunning = false; isExit = true; taskWorkerLock.notifyAll(); } }