org.openide.util.Task类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(87)

本文整理了Java中org.openide.util.Task类的一些代码示例,展示了Task类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Task类的具体详情如下:
包路径:org.openide.util.Task
类名称:Task

Task介绍

[英]A task that may be executed in a separate thread and permits examination of its status. Other threads can check if it is finished or wait for it to finish.

For example:

``

Runnable r = new Runnable () { 
public void run () { 
// do something 
} 
}; 
Task task = new Task (r); 
RequestProcessor.postRequest (task);

In a different thread one can then test task.isFinished () or wait for it with task.waitFinished ().
[中]一种可以在单独线程中执行并允许检查其状态的任务。其他线程可以检查它是否完成,或者等待它完成。
例如:
``

Runnable r = new Runnable () { 
public void run () { 
// do something 
} 
}; 
Task task = new Task (r); 
RequestProcessor.postRequest (task);

在另一个线程中,可以测试task.isFinished ()或使用[$2$]等待它。

代码示例

代码示例来源:origin: org.netbeans.api/org-openide-util

return super.waitFinished(timeout);
  } else {
    return true;
return super.waitFinished(timeout);

代码示例来源:origin: org.netbeans.api/org-openide-util

/** Delegates valid cancel requests to asociated AsyncGUIJob, in the case
 * job supports cancelling. */
private void cancel() {
  if ((initTask != null) && !initTask.isFinished() && (initJob instanceof Cancellable)) {
    synchronized (CANCELLED_LOCK) {
      LOG.log(Level.FINE, "Cancelling for {0}", comp4Init);
      wasCancelled = true;
    }
    ((Cancellable) initJob).cancel();
    LOG.fine("Cancelling done");
  }
}

代码示例来源:origin: stackoverflow.com

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.invokeAll(Arrays.asList(new Task()), 10, TimeUnit.MINUTES); // Timeout of 10 minutes.
executor.shutdown();

代码示例来源:origin: org.netbeans.api/org-openide-util

/** Start the task.
* When it finishes (even with an exception) it calls
* {@link #notifyFinished}.
* Subclasses may override this method, but they
* then need to call {@link #notifyFinished} explicitly.
* <p>Note that this call runs synchronously, but typically the creator
* of the task will call this method in a separate thread.
*/
public void run() {
  try {
    notifyRunning();
    if (run != null) {
      run.run();
    }
  } finally {
    notifyFinished();
  }
}

代码示例来源:origin: stackoverflow.com

ExecutorService exec = Executors.newFixedThreadPool(10);

for(int i = 0; i < 10; i++) {
  exec.submit(new Task(i));
}    

// later

for(int i = 0; i < 10; i++) {
  exec.submit(new Task(i));
}    

exec.shutdown(); // etc.

代码示例来源:origin: stackoverflow.com

ExecutorService es = Executors.newFixedThreadPool(3);
for(int i=0;i<10;i++)
  es.submit(new Task(i));

代码示例来源:origin: stackoverflow.com

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(new Task());

try {
  System.out.println("Started");
  Integer retval = future.get(10, TimeUnit.SECONDS)); // you can choose your desired time here
  System.out.println("Finished");
} catch (TimeoutException e) {
  future.cancel(true);
  System.out.println("Timeout happened");
  // handle termination here
}

executor.shutdownNow();

代码示例来源:origin: stackoverflow.com

public class MyTask implements Runnable
{
  @Override
  public void run()
  {
    ...
  }
}

ExecutorService s = Executors.newSingleThreadExecutor();
Future<?> submit = s.submit(new Task());

代码示例来源:origin: stackoverflow.com

ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0; i < LARGE_NUMBER; i++) {
  exec.submit(new Task(i));
}

exec.awaitTermination(1000, TimeUnit.HOURS); // wait for all tasks to finish

代码示例来源:origin: stackoverflow.com

ExecutorService executor = Executors.newFixedThreadPool(tasks.size());

for (final Task task : tasks) {
  executor.submit(new Runnable() {
    @Override
    public void run() {
      task.run(args);
    }
  });
}

executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLSECONDS);

代码示例来源:origin: stackoverflow.com

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
List<Future<String>> results = executor.invokeAll(Arrays.asList(new Task()), 1, TimeUnit.SECONDS); // Timeout of 1 second.
for (Future<String> result : results) {
  if (!result.isCancelled()) {
    System.out.println(result.get()); // Won't be printed as the "actual" processing took 2 seconds.
  } else {
    System.out.println("Task timed out.");
  }
}
executor.shutdown();

代码示例来源:origin: stackoverflow.com

@WebListener
public class Config implements ServletContextListener {

  private ScheduledExecutorService scheduler;

  @Override
  public void contextInitialized(ServletContextEvent event) {
    scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Task(), 0, 1, TimeUnit.MINUTES); // Schedule to run every minute.
  }

  @Override
  public void contextDestroyed(ServletContextEvent event) {
    scheduler.shutdown(); // Important! This stops the thread.
  }

}

代码示例来源:origin: stackoverflow.com

ExecutorService service = Executors.newCachedThreadPool();
// My task
Task task = new Task();
// Submit the task and get the corresponding future
Future<?> future = service.submit(task);
...
// Cancel the task which will interrupt the thread that was executing the 
// task if any
future.cancel(true);

代码示例来源:origin: stackoverflow.com

ExecutorService executor = Executors.newFixedThreadPool(2);
 CompletionService<String> compService = new ExecutorCompletionService<>(executor);
 for (int i = 0; i < 2; i++) {
   compService.submit(new Task(i));
 }
 for (int i = 0; i < 2; i++) {
   try {
     String result = compService.take().get();
     System.out.println("RESULT: " + result);
   } catch (ExecutionException | InterruptedException e) {
     e.printStackTrace();
   }
 }
 executor.shutdownNow();

代码示例来源:origin: stackoverflow.com

Timer timer = new Timer();
timer.schedule(new Task(), 1000); //schedule the task to be run at 1 second (1000 mili sec) time

代码示例来源:origin: stackoverflow.com

if (!timerRunning) {
    mcont=this;
    myTimer = new Timer();
    myTimer.scheduleAtFixedRate(new Task(), START_TIME, RETRY_TIME);
    timerRunning = true;
  }

代码示例来源:origin: stackoverflow.com

// "Wrapped" (and thus shared) by task0 and task1 below.
Runnable runnable = new Runnable() {
  @Override
  public void run() {
    System.out.println("task was invoked");
  }
}


Timer timer0 = new Timer();
Task task0 = new Task(runnable);
timer0.schedule(task0, delay);
timer0.cancel();

Task task1 = new Task(runnable);
Timer timer1 = new Timer();
timer1.schedule(task1, delay); // throws an exception if we use task0

Thread.sleep(5000);
timer1.cancel();

代码示例来源:origin: stackoverflow.com

float delay = 1; // seconds

Timer.schedule(new Task(){
  @Override
  public void run() {
    // Do your work
  }
}, delay);

代码示例来源:origin: stackoverflow.com

ExecutorService executor = Executors.newFixedThreadPool(3);
CompletionService<Void> compService = new ExecutorCompletionService<Void>(executor);

for(int i = 0; i < 3; i++) compService.submit(new Task(), null);

while(true)
{
  compService.take(); // wait until one of the threads finishes its task
  compService.submit(new Task(), null);
}

代码示例来源:origin: stackoverflow.com

final ExecutorService executorService = Executors.newCachedThreadPool();
final ExecutorCompletionService<String> completionService = 
      new ExecutorCompletionService<String>(executorService);
for (int i = 0; i < 10; ++i) {
  completionService.submit(new Task());
}
completionService.take().get();

相关文章