1. /*
  2. * @(#)Executor.java 1.5 04/02/09
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent;
  8. /**
  9. * An object that executes submitted {@link Runnable} tasks. This
  10. * interface provides a way of decoupling task submission from the
  11. * mechanics of how each task will be run, including details of thread
  12. * use, scheduling, etc. An <tt>Executor</tt> is normally used
  13. * instead of explicitly creating threads. For example, rather than
  14. * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
  15. * of a set of tasks, you might use:
  16. *
  17. * <pre>
  18. * Executor executor = <em>anExecutor</em>
  19. * executor.execute(new RunnableTask1());
  20. * executor.execute(new RunnableTask2());
  21. * ...
  22. * </pre>
  23. *
  24. * However, the <tt>Executor</tt> interface does not strictly
  25. * require that execution be asynchronous. In the simplest case, an
  26. * executor can run the submitted task immediately in the caller's
  27. * thread:
  28. *
  29. * <pre>
  30. * class DirectExecutor implements Executor {
  31. * public void execute(Runnable r) {
  32. * r.run();
  33. * }
  34. * }</pre>
  35. *
  36. * More typically, tasks are executed in some thread other
  37. * than the caller's thread. The executor below spawns a new thread
  38. * for each task.
  39. *
  40. * <pre>
  41. * class ThreadPerTaskExecutor implements Executor {
  42. * public void execute(Runnable r) {
  43. * new Thread(r).start();
  44. * }
  45. * }</pre>
  46. *
  47. * Many <tt>Executor</tt> implementations impose some sort of
  48. * limitation on how and when tasks are scheduled. The executor below
  49. * serializes the submission of tasks to a second executor,
  50. * illustrating a composite executor.
  51. *
  52. * <pre>
  53. * class SerialExecutor implements Executor {
  54. * final Queue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();
  55. * final Executor executor;
  56. * Runnable active;
  57. *
  58. * SerialExecutor(Executor executor) {
  59. * this.executor = executor;
  60. * }
  61. *
  62. * public synchronized void execute(final Runnable r) {
  63. * tasks.offer(new Runnable() {
  64. * public void run() {
  65. * try {
  66. * r.run();
  67. * } finally {
  68. * scheduleNext();
  69. * }
  70. * }
  71. * });
  72. * if (active == null) {
  73. * scheduleNext();
  74. * }
  75. * }
  76. *
  77. * protected synchronized void scheduleNext() {
  78. * if ((active = tasks.poll()) != null) {
  79. * executor.execute(active);
  80. * }
  81. * }
  82. * }</pre>
  83. *
  84. * The <tt>Executor</tt> implementations provided in this package
  85. * implement {@link ExecutorService}, which is a more extensive
  86. * interface. The {@link ThreadPoolExecutor} class provides an
  87. * extensible thread pool implementation. The {@link Executors} class
  88. * provides convenient factory methods for these Executors.
  89. *
  90. * @since 1.5
  91. * @author Doug Lea
  92. */
  93. public interface Executor {
  94. /**
  95. * Executes the given command at some time in the future. The command
  96. * may execute in a new thread, in a pooled thread, or in the calling
  97. * thread, at the discretion of the <tt>Executor</tt> implementation.
  98. *
  99. * @param command the runnable task
  100. * @throws RejectedExecutionException if this task cannot be
  101. * accepted for execution.
  102. * @throws NullPointerException if command is null
  103. */
  104. void execute(Runnable command);
  105. }