/** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the {@code Executor} implementation. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be * accepted for execution * @throws NullPointerException if command is null */ voidexecute(Runnable command); }
The {@code Executor} implementations provided in thispackage implement {@link ExecutorService}, which is a more extensive interface. The{@link ThreadPoolExecutor} classprovidesan extensiblethreadpoolimplementation. The{@link Executors} class providesconvenientfactorymethodsfortheseExecutors.
@Test publicvoidtest2(){ ExecutorService pool = Executors.newSingleThreadExecutor(); for (int i = 0;i<10;i++) { Runnable task = new myTask(); pool.submit(task);
@Test publicvoidtest4(){ ExecutorService pool = Executors.newCachedThreadPool(); for (int i = 0;i<100;i++) { Runnable task = new myTask(); pool.submit(task);
/** * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity. * * @param capacity the capacity of this queue * @throws IllegalArgumentException if {@code capacity} is not greater * than zero */ publicLinkedBlockingQueue(int capacity){ if (capacity <= 0) thrownew IllegalArgumentException(); this.capacity = capacity; last = head = new Node<E>(null); }
/** * Creates a {@code LinkedBlockingQueue} with a capacity of * {@link Integer#MAX_VALUE}, initially containing the elements of the * given collection, * added in traversal order of the collection's iterator. * * @param c the collection of elements to initially contain * @throws NullPointerException if the specified collection or any * of its elements are null */ publicLinkedBlockingQueue(Collection<? extends E> c){ this(Integer.MAX_VALUE); final ReentrantLock putLock = this.putLock; putLock.lock(); // Never contended, but necessary for visibility try { int n = 0; for (E e : c) { if (e == null) thrownew NullPointerException(); if (n == capacity) thrownew IllegalStateException("Queue full"); enqueue(new Node<E>(e)); ++n; } count.set(n); } finally { putLock.unlock(); } }
/** * Creates an {@code ArrayBlockingQueue} with the given (fixed) * capacity and the specified access policy. * * @param capacity the capacity of this queue * @param fair if {@code true} then queue accesses for threads blocked * on insertion or removal, are processed in FIFO order; * if {@code false} the access order is unspecified. * @throws IllegalArgumentException if {@code capacity < 1} */ publicArrayBlockingQueue(int capacity, boolean fair){ if (capacity <= 0) thrownew IllegalArgumentException(); this.items = new Object[capacity]; lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition(); }