Skip to content

Commit

Permalink
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Browse files Browse the repository at this point in the history
8257671: ThreadPoolExecutor.Discard*Policy: rejected tasks are not cancelled

Reviewed-by: alanb, prappo, dl
  • Loading branch information
Martin Buchholz committed Jan 9, 2021
1 parent 63e3bd7 commit 270014a
Show file tree
Hide file tree
Showing 41 changed files with 273 additions and 207 deletions.
Expand Up @@ -54,13 +54,13 @@
* to return {@code RunnableFuture} implementations other than
* {@code FutureTask}.
*
* <p><b>Extension example</b>. Here is a sketch of a class
* <p><b>Extension example.</b> Here is a sketch of a class
* that customizes {@link ThreadPoolExecutor} to use
* a {@code CustomTask} class instead of the default {@code FutureTask}:
* <pre> {@code
* public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
*
* static class CustomTask<V> implements RunnableFuture<V> {...}
* static class CustomTask<V> implements RunnableFuture<V> { ... }
*
* protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
* return new CustomTask<V>(c);
Expand Down
Expand Up @@ -69,7 +69,7 @@
* perform some action upon state updates.
*
* <pre> {@code
* class Handler { void handle(); ... }
* class Handler { void handle() { ... } }
*
* class X {
* private final CopyOnWriteArraySet<Handler> handlers
Expand Down
Expand Up @@ -118,7 +118,7 @@
* class Driver2 { // ...
* void main() throws InterruptedException {
* CountDownLatch doneSignal = new CountDownLatch(N);
* Executor e = ...
* Executor e = ...;
*
* for (int i = 0; i < N; ++i) // create and start threads
* e.execute(new WorkerRunnable(doneSignal, i));
Expand All @@ -135,10 +135,8 @@
* this.i = i;
* }
* public void run() {
* try {
* doWork(i);
* doneSignal.countDown();
* } catch (InterruptedException ex) {} // return;
* doWork();
* doneSignal.countDown();
* }
*
* void doWork() { ... }
Expand Down
Expand Up @@ -94,7 +94,9 @@
*
* // wait until done
* for (Thread thread : threads)
* thread.join();
* try {
* thread.join();
* } catch (InterruptedException ex) { }
* }
* }}</pre>
*
Expand Down
Expand Up @@ -57,8 +57,8 @@
* <pre> {@code
* class FillAndEmpty {
* Exchanger<DataBuffer> exchanger = new Exchanger<>();
* DataBuffer initialEmptyBuffer = ... a made-up type
* DataBuffer initialFullBuffer = ...
* DataBuffer initialEmptyBuffer = ...; // a made-up type
* DataBuffer initialFullBuffer = ...;
*
* class FillingLoop implements Runnable {
* public void run() {
Expand All @@ -69,7 +69,7 @@
* if (currentBuffer.isFull())
* currentBuffer = exchanger.exchange(currentBuffer);
* }
* } catch (InterruptedException ex) { ... handle ... }
* } catch (InterruptedException ex) { ... handle ...}
* }
* }
*
Expand Down
Expand Up @@ -117,7 +117,7 @@
* if (!pool.awaitTermination(60, TimeUnit.SECONDS))
* System.err.println("Pool did not terminate");
* }
* } catch (InterruptedException ie) {
* } catch (InterruptedException ex) {
* // (Re-)Cancel if current thread also interrupted
* pool.shutdownNow();
* // Preserve interrupt status
Expand Down
39 changes: 21 additions & 18 deletions src/java.base/share/classes/java/util/concurrent/Future.java
Expand Up @@ -56,8 +56,8 @@
* <pre> {@code
* interface ArchiveSearcher { String search(String target); }
* class App {
* ExecutorService executor = ...
* ArchiveSearcher searcher = ...
* ExecutorService executor = ...;
* ArchiveSearcher searcher = ...;
* void showSearch(String target) throws InterruptedException {
* Callable<String> task = () -> searcher.search(target);
* Future<String> future = executor.submit(task);
Expand Down Expand Up @@ -88,25 +88,28 @@
public interface Future<V> {

/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when {@code cancel} is called,
* this task should never run. If the task has already started,
* then the {@code mayInterruptIfRunning} parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task.
* Attempts to cancel execution of this task. This method has no
* effect if the task is already completed or cancelled, or could
* not be cancelled for some other reason. Otherwise, if this
* task has not started when {@code cancel} is called, this task
* should never run. If the task has already started, then the
* {@code mayInterruptIfRunning} parameter determines whether the
* thread executing this task (when known by the implementation)
* is interrupted in an attempt to stop the task.
*
* <p>After this method returns, subsequent calls to {@link #isDone} will
* always return {@code true}. Subsequent calls to {@link #isCancelled}
* will always return {@code true} if this method returned {@code true}.
* <p>The return value from this method does not necessarily
* indicate whether the task is now cancelled; use {@link
* #isCancelled}.
*
* @param mayInterruptIfRunning {@code true} if the thread executing this
* task should be interrupted; otherwise, in-progress tasks are allowed
* to complete
* @param mayInterruptIfRunning {@code true} if the thread
* executing this task should be interrupted (if the thread is
* known to the implementation); otherwise, in-progress tasks are
* allowed to complete
* @return {@code false} if the task could not be cancelled,
* typically because it has already completed normally;
* {@code true} otherwise
* typically because it has already completed; {@code true}
* otherwise. If two or more threads cause a task to be cancelled,
* then at least one of them returns {@code true}. Implementations
* may provide stronger guarantees.
*/
boolean cancel(boolean mayInterruptIfRunning);

Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/concurrent/Phaser.java
Expand Up @@ -245,7 +245,7 @@
* be appropriate for extremely small per-phase task bodies (thus
* high rates), or up to hundreds for extremely large ones.
*
* <p><b>Implementation notes</b>: This implementation restricts the
* <p><b>Implementation notes:</b> This implementation restricts the
* maximum number of parties to 65535. Attempts to register additional
* parties result in {@code IllegalStateException}. However, you can and
* should create tiered phasers to accommodate arbitrarily large sets
Expand Down Expand Up @@ -919,7 +919,7 @@ public boolean isTerminated() {
* <pre> {@code
* Phaser phaser = new Phaser() {
* protected boolean onAdvance(int phase, int parties) { return false; }
* }}</pre>
* };}</pre>
*
* @param phase the current phase number on entry to this method,
* before this phaser is advanced
Expand Down
Expand Up @@ -87,7 +87,7 @@
* <pre> {@code
* class FIFOEntry<E extends Comparable<? super E>>
* implements Comparable<FIFOEntry<E>> {
* static final AtomicLong seq = new AtomicLong(0);
* static final AtomicLong seq = new AtomicLong();
* final long seqNum;
* final E entry;
* public FIFOEntry(E entry) {
Expand Down Expand Up @@ -290,7 +290,9 @@ private void tryGrow(Object[] array, int oldCap) {
if (allocationSpinLock == 0 &&
ALLOCATIONSPINLOCK.compareAndSet(this, 0, 1)) {
try {
int growth = oldCap < 64 ? oldCap + 2 : oldCap >> 1;
int growth = (oldCap < 64)
? (oldCap + 2) // grow faster if small
: (oldCap >> 1);
int newCap = ArraysSupport.newLength(oldCap, 1, growth);
if (queue == array)
newArray = new Object[newCap];
Expand Down
Expand Up @@ -66,7 +66,7 @@
*
* // Not a particularly efficient data structure; just for demo
*
* protected Object[] items = ... whatever kinds of items being managed
* protected Object[] items = ...; // whatever kinds of items being managed
* protected boolean[] used = new boolean[MAX_AVAILABLE];
*
* protected synchronized Object getNextAvailableItem() {
Expand Down

1 comment on commit 270014a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.