@Test public void testHighLoad() { final int NUMTHREADS = 100; LinkedList<SimpleTask> runnables = new LinkedList<SimpleTask>(); ThreadHandler th = ThreadHandler.getInstance(); ArrayList<SimpleTask> threadList = new ArrayList<SimpleTask>(NUMTHREADS); for (int i = 0; i < NUMTHREADS; ++i) { SimpleTask mod = new SimpleSuspendableTask(new SuspendableTestThread((int) (Math.random() * 100))); threadList.add(mod); runnables.add(mod); if ((Math.random() > 0.75) || (i == (NUMTHREADS - 1))) { th.scheduleTasks(runnables); runnables.clear(); } sleep((int) (Math.random() * 50)); assertTrue(Runtime.getRuntime().availableProcessors() >= th.d_runningTasks.size()); } assertTrue(Runtime.getRuntime().availableProcessors() >= th.d_runningTasks.size()); waitTillDone(); for (SimpleTask mod : threadList) { assertTrue(mod.isFinished()); } }
private void startTaskAction() { try { _task.execute(); } catch (TaskException e) { sout(e.getMessage()); } }
@Test public void testRemoveSuspendableRunningTask() { waitTillDone(); ThreadHandler th = ThreadHandler.getInstance(); SimpleTask task = new SimpleSuspendableTask(new SuspendableTestThread(10000)); th.scheduleTask(task); sleepLongEnough(); assertEquals(1, th.getRunningThreads()); th.abortTask(task); sleepLongEnough(); assertEquals(0, th.getRunningThreads()); assertFalse(task.isFinished()); assertTrue(task.isAborted()); assertFalse(task.isSuspended()); }
@Test public void testRemoveWaitingSimpleTask() { waitTillDone(); ThreadHandler th = ThreadHandler.getInstance(); List<SimpleTask> runthreads = new ArrayList<SimpleTask>(); SimpleTask t = new SimpleSuspendableTask(new SuspendableTestThread(100)); for (int i = 0; i < d_numCores + 1; ++i) { runthreads.add(new SimpleSuspendableTask(new SuspendableTestThread(100))); } runthreads.add(t); // 2 threads in waiting list. th.scheduleTasks(runthreads); assertEquals(d_numCores + 2, th.getQueuedTasks()); th.abortTask(t); assertEquals(d_numCores + 1, th.getQueuedTasks()); waitTillDone(); assertFalse(t.isFinished()); }
@Test public void testRemoveCompositeTaskWithDuplicateTask() { ThreadHandler threadHandler = ThreadHandler.getInstance(); SomeCompositeTask task = new SomeCompositeTask(); SimpleTask duplicate = task.d_first; threadHandler.scheduleTask(duplicate); threadHandler.scheduleTask(task); sleepLongEnough(); List<Task> expected = new ArrayList<Task>(); expected.add(task); expected.add(duplicate); assertEquals(Collections.singletonList(duplicate), threadHandler.getRunningTasks()); assertEquals(expected, threadHandler.getQueuedTaskList()); threadHandler.abortTask(task); sleepLongEnough(); assertFalse(duplicate.isFinished()); expected.remove(task); assertEquals(expected, threadHandler.getQueuedTaskList()); assertEquals(Collections.singletonList(duplicate), threadHandler.getRunningTasks()); }
public List<SimpleTask> getNextTasks() { if (!d_started) { throw new RuntimeException("Can't get tasks if not started"); } else if (!d_first.isFinished()) { return Collections.singletonList(d_first); } else if (!d_middle1.isFinished() || !d_middle2.isFinished()) { List<SimpleTask> list = new ArrayList<SimpleTask>(); if (!d_middle1.isFinished()) list.add(d_middle1); if (!d_middle2.isFinished()) list.add(d_middle2); return list; } else if (!d_last.isFinished()) { return Collections.singletonList(d_last); } else { return Collections.<SimpleTask>emptyList(); } }
/** create a test task and wire it up with a task handler that dumps output to the textarea */ @SuppressWarnings("unchecked") private void _setupTask() { TaskExecutorIF<ByteBuffer> functor = new TaskExecutorAdapter<ByteBuffer>() { public ByteBuffer doInBackground(Future<ByteBuffer> swingWorker, SwingUIHookAdapter hook) throws Exception { _initHook(hook); // set the license key MapLookup.setLicenseKey(ttfLicense.getText()); // get the uri for the static map String uri = MapLookup.getMap( Double.parseDouble(ttfLongi.getText()), Double.parseDouble(ttfLati.getText()), Integer.parseInt(ttfSizeW.getText()), Integer.parseInt(ttfSizeH.getText()), Integer.parseInt(ttfZoom.getText())); sout("Google Maps URI=" + uri); // get the map from Google GetMethod get = new GetMethod(uri); new HttpClient().executeMethod(get); ByteBuffer data = HttpUtils.getMonitoredResponse(hook, get); try { _img = ImageUtils.toCompatibleImage(ImageIO.read(data.getInputStream())); sout("converted downloaded data to image..."); } catch (Exception e) { _img = null; sout("The URI is not an image. Data is downloaded, can't display it as an image."); _respStr = new String(data.getBytes()); } return data; } @Override public String getName() { return _task.getName(); } }; _task = new SimpleTask( new TaskManager(), functor, "HTTP GET Task", "Download an image from a URL", AutoShutdownSignals.Daemon); _task.addStatusListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { sout(":: task status change - " + ProgressMonitorUtils.parseStatusMessageFrom(evt)); lblProgressStatus.setText(ProgressMonitorUtils.parseStatusMessageFrom(evt)); } }); _task.setTaskHandler( new SimpleTaskHandler<ByteBuffer>() { @Override public void beforeStart(AbstractTask task) { sout(":: taskHandler - beforeStart"); } @Override public void started(AbstractTask task) { sout(":: taskHandler - started "); } /** {@link SampleApp#_initHook} adds the task status listener, which is removed here */ @Override public void stopped(long time, AbstractTask task) { sout(":: taskHandler [" + task.getName() + "]- stopped"); sout(":: time = " + time / 1000f + "sec"); task.getUIHook().clearAllStatusListeners(); } @Override public void interrupted(Throwable e, AbstractTask task) { sout(":: taskHandler [" + task.getName() + "]- interrupted - " + e.toString()); } @Override public void ok(ByteBuffer value, long time, AbstractTask task) { sout( ":: taskHandler [" + task.getName() + "]- ok - size=" + (value == null ? "null" : value.toString())); if (_img != null) { sout("********************************"); sout(" INSTRUCTIONS"); sout("********************************"); sout("Click anywhere on the loaded"); sout("map and it will save the"); sout("coordinates and open up a new"); sout("window with those coordinates."); sout(""); sout("The clicked coordinates are"); sout("saved under a combo box in"); sout("this window under get quit."); sout("********************************"); _displayImgInFrame(); } else _displayRespStrInFrame(); } @Override public void error(Throwable e, long time, AbstractTask task) { sout(":: taskHandler [" + task.getName() + "]- error - " + e.toString()); } @Override public void cancelled(long time, AbstractTask task) { sout(" :: taskHandler [" + task.getName() + "]- cancelled"); } }); }
private void quitProgram() { _task.shutdown(); System.exit(0); }
public boolean isFinished() { return d_last.isFinished(); }