@Override public Double doInBackground() { LOG.debug("Building threads"); for (int i = 0; i < threadCount; i++) { threads.add(new PerformanceThread()); } time = time * 1000; double stepsPerSecondAll = 0; double pi = 0; long start = System.currentTimeMillis(); long stepsOverall = 0; Timer timer = new Timer(); timer.schedule(new PerformanceTimerTask(start), 0, intervall); LOG.debug("starting threads"); for (PerformanceThread p : threads) { p.start(); } while ((System.currentTimeMillis() - start < time) && !isCancelled()) { try { Thread.sleep(intervall / 10); } catch (InterruptedException ex) { LOG.debug("", ex); } } timer.cancel(); setProgress(100); LOG.debug("Interrupting threads"); for (PerformanceThread p : threads) { p.interrupt(); stepsOverall += p.getSteps(); pi += p.getPi(); } pi = pi / threadCount; stepsPerSecondAll = stepsOverall / (System.currentTimeMillis() - start); publish(pi + " ( " + stepsPerSecondAll + " steps per millisecond )"); return pi; }
void start() { if (started.compareAndSet(false, true)) { thread.start(); } }