@Override public boolean cancel() { LongTaskExecutor.this.cancel(); return true; } });
/** * Execute a long task with cancel and progress support. Task can be * <code>null</code>. In this case <code>runnable</code> will be executed * normally, but without cancel and progress support. * * @param task the task to be executed, can be <code>null</code>. * @param runnable the runnable to be executed * @throws NullPointerException if <code>runnable</code> is null * @throws IllegalStateException if a task is still executing at this time */ public synchronized void execute(LongTask task, Runnable runnable) { execute(task, runnable, "", null); }
@Override public boolean isRunning() { return executor.isRunning(); }
@Override public void actionPerformed(ActionEvent e) { LongTaskExecutor executor = new LongTaskExecutor(true); LongTaskExample longTaskExample = new LongTaskExample(); executor.execute(longTaskExample, longTaskExample, "Task...", null); }
public DesktopGeneratorController() { executor = new LongTaskExecutor(true, "Generator"); }
public LayoutModelImpl(Workspace workspace) { this.workspace = workspace; listeners = new ArrayList<>(); savedProperties = new HashMap<>(); executor = new LongTaskExecutor(true, "layout", 5); executor.setLongTaskListener(new LongTaskListener() { @Override public void taskFinished(LongTask task) { setRunning(false); } }); executor.setDefaultErrorHandler(new LongTaskErrorHandler() { @Override public void fatalError(Throwable t) { Exceptions.printStackTrace(t); } }); }
/** * Execute a long task with cancel and progress support. Task can be * <code>null</code>. In this case <code>runnable</code> will be executed * normally, but without cancel and progress support. * * @param task the task to be executed, can be <code>null</code>. * @param runnable the runnable to be executed * @param taskName the name of the task, is displayed in the status bar if * available * @param errorHandler error handler for exception retrieval during * execution * @throws NullPointerException if <code>runnable</code> * or * <code>taskName</code> is null * @throws IllegalStateException if a task is still executing at this time */ public synchronized void execute(LongTask task, final Runnable runnable, String taskName, LongTaskErrorHandler errorHandler) { if (runnable == null || taskName == null) { throw new NullPointerException(); } if (executor == null) { this.executor = new ThreadPoolExecutor(0, 1, 15, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory()); } RunningLongTask runningLongtask = new RunningLongTask(task, runnable, taskName, errorHandler); if (inBackground) { runningLongtask.future = executor.submit(runningLongtask); } else { currentTask = runningLongtask; runningLongtask.run(); } }
private synchronized void finished(RunningLongTask runningLongTask) { if (cancelTimer != null) { cancelTimer.cancel(); } LongTask task = runningLongTask.task; currentTask = null; if (listener != null) { listener.taskFinished(task); } }
/** * Cancel the current task. If the task fails to cancel itself and if an * <code>interruptDelay</code> has been specified, the task will be * <b>interrupted</b> after <code>interruptDelay</code>. Using * <code>Thread.interrupt()</code> may cause hazardous behaviors and should * be avoided. Therefore any task should be cancelable. */ public synchronized void cancel() { if (inBackground) { if (executor != null) { RunningLongTask rlt = (RunningLongTask) currentTask; if (rlt != null) { boolean res = rlt.cancel(); if (interruptCancel && !res) { cancelTimer = new Timer(name + "_cancelTimer"); cancelTimer.schedule(new InterruptTimerTask(rlt), interruptDelay); } } } } else { RunningLongTask rlt = (RunningLongTask) currentTask; if (rlt != null) { boolean res = rlt.cancel(); if (interruptCancel && !res) { cancelTimer = new Timer(name + "_cancelTimer"); cancelTimer.schedule(new InterruptTimerTask(rlt), interruptDelay); } } } }
@Override public void run() { currentTask = this; try { runnable.run(); } catch (Throwable e) { LongTaskErrorHandler err = errorHandler; finished(this); if (progress != null) { progress.finish(); } if (err != null) { err.fatalError(e); } else if (defaultErrorHandler != null) { defaultErrorHandler.fatalError(e); } else { Logger.getLogger("").log(Level.SEVERE, "", e); } } currentTask = null; finished(this); if (progress != null) { progress.finish(); } }
public void takeScreenshot() { executor.execute(this, this, NbBundle.getMessage(ScreenshotMaker.class, "ScreenshotMaker.progress.message"), null); }
public DesktopImportControllerUI() { controller = Lookup.getDefault().lookup(ImportController.class); errorHandler = new LongTaskErrorHandler() { @Override public void fatalError(Throwable t) { if (t instanceof OutOfMemoryError) { return; } Exceptions.printStackTrace(t); } }; executor = new LongTaskExecutor(true, "Importer", 10); }
@Override public void stopLayout() { model.getExecutor().cancel(); }
@Override public void taskFinished(LongTask task) { model.setRunning(statistics, false); for (StatisticsUI s : uis) { model.addResult(s); s.unsetup(); } if (listener != null) { listener.taskFinished(statistics instanceof LongTask ? (LongTask) statistics : null); } } });
@Override public void exportFile(final FileObject fileObject, final Exporter exporter) { if (exporter == null) { throw new RuntimeException(NbBundle.getMessage(getClass(), "error_no_matching_file_exporter")); } //Export Task LongTask task = null; if (exporter instanceof LongTask) { task = (LongTask) exporter; } String taskmsg = NbBundle.getMessage(DesktopExportController.class, "DesktopExportController.exportTaskName", fileObject.getNameExt()); executor.execute(task, new Runnable() { @Override public void run() { try { controller.exportFile(FileUtil.toFile(fileObject), exporter); StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(DesktopExportController.class, "DesktopExportController.status.exportSuccess", fileObject.getNameExt())); } catch (Exception ex) { throw new RuntimeException(ex); } } }, taskmsg, errorHandler); }
public DesktopExportController() { controller = Lookup.getDefault().lookup(ExportController.class); errorHandler = new LongTaskErrorHandler() { @Override public void fatalError(Throwable t) { Exceptions.printStackTrace(t); } }; executor = new LongTaskExecutor(true, "Exporter", 10); }
@Override public void close(Workspace workspace) { LayoutModelImpl layoutModel = workspace.getLookup().lookup(LayoutModelImpl.class); if (layoutModel != null) { layoutModel.getExecutor().cancel(); } }
@Override public void executeLayout() { if (model.getSelectedLayout() != null) { layoutRun = new LayoutRun(model.getSelectedLayout()); model.getExecutor().execute(layoutRun, layoutRun); model.setRunning(true); } }
public ScreenshotMaker() { //Preferences String lastPathDefault = NbPreferences.forModule(ScreenshotMaker.class).get(LAST_PATH_DEFAULT, null); defaultDirectory = NbPreferences.forModule(ScreenshotMaker.class).get(LAST_PATH, lastPathDefault); antiAliasing = NbPreferences.forModule(ScreenshotMaker.class).getInt(ANTIALIASING_DEFAULT, antiAliasing); width = NbPreferences.forModule(ScreenshotMaker.class).getInt(WIDTH_DEFAULT, width); height = NbPreferences.forModule(ScreenshotMaker.class).getInt(HEIGHT_DEFAULT, height); transparentBackground = NbPreferences.forModule(ScreenshotMaker.class).getBoolean(TRANSPARENT_BACKGROUND_DEFAULT, transparentBackground); autoSave = NbPreferences.forModule(ScreenshotMaker.class).getBoolean(AUTOSAVE_DEFAULT, autoSave); finishedMessage = NbPreferences.forModule(ScreenshotMaker.class).getBoolean(SHOW_MESSAGE, finishedMessage); executor = new LongTaskExecutor(true, "Screenshot Maker"); tileWidth = width / 16; tileHeight = height / 12; }
@Override public void executeLayout(int numIterations) { if (model.getSelectedLayout() != null) { layoutRun = new LayoutRun(model.getSelectedLayout(), numIterations); model.getExecutor().execute(layoutRun, layoutRun); model.setRunning(true); } }