From fa0743c32338f147eaf7a5d69566bbc15d193f85 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Fri, 26 Sep 2014 12:05:37 +0200 Subject: Add missing canonical combinators: - `def transform[S](f: Try[T] => Try[S])(implicit executor: ExecutionContext): Future[S]` - `def transformWith[S](f: Try[T] => Future[S])(implicit executor: ExecutionContext): Future[S]` - `def flatten[S](implicit ev: T <:< Future[S]): Future[S]` - `def zipWith[U, R](that: Future[U])(f: (T, U) => R)(implicit executor: ExecutionContext): Future[R]` Add missing utilities: - `val unit: Future[Unit]` in `object Future` - `object never extends Future[Nothing]` in `object Future` - `def defaultBlockContext: BlockContext` in `object BlockContext` - `def toString: String` on stdlib implementations of `Future` Refactors: - the `scala.concurrent.Future` trait to not explicit create any `Promises`, so that implementations can control implementation type, this is mainly facilitated through adding of the `transform` and `transformWith` methods. - the implementation of `ExecutionContextImpl` has been cleaned up - the `scala.concurrent.impl.DefaultPromise` has been reimplemented to not use `sun.misc.Unsafe` Securing: - Add a self-check in `completeWith` and `tryCompleteWith` to avoid cycles in trait Promise - Capping the maximum number of threads for the global `ExecutionContext` to the max parallelism - Implementing (almost) all `Future` combinators on `transformWith` and `transform` means that `DefaultPromise` linking works on both `(flat)map` and `recover(With)` - Nested `blocking {}` should not spawn extra threads beyond the first. Removes: - the private `internalExecutor` method in favor of an import in trait `Future` - the private `internalExecutor` method in favor of an import in trait `Promise` - the `AtomicReferenceFieldUpdater` in `AbstractPromise` since we're using `Unsafe` - `scala.concurrent.impl.Future` is no longer needed Deprecates: - `Future.onSuccess` - discourage the use of callbacks (and is also redundant considering `foreach` and `onComplete`) - `Future.onFailure` - discourage the use of callbacks (and is also redundant considering `onComplete` and `failed.foreach`) - `ExecutionContext.prepare` - it was ill specced and it is too easy to forget to call it (or even know when to call it or call it more times than needed) - All classes in scala.concurrent.forkjoin. Scala 2.12 will be Java 8+ and as such the jsr166e should be used as included in java.util.concurrent. Reimplements: - `failed` - in terms of `transform` - `map` - in terms of `transform` - `flatMap` - in terms of `transformWith` - `recover` - in terms of `transform` - `recoverWith` - in terms of `transformWith` - `zip` - in terms of `flatMap` + `map` - `fallbackTo` - in terms of `recoverWith` + `recoverWith` - `andThen` - in terms of `transform` Miscellaneous: - Giving the threads of `ExecutionContext.global` sensible names - Optimizes `object Future.successful` and `object Future.failed` are now separate implementations, to optimize for the result, avoiding doing work for the "other branch". - Optimizes `compressedRoot()` by avoiding double-calls to volatile get. Documentation: - Almost all methods on `Future` and `Promise` have been revisited and had their ScalaDoc updated Tests: - Yes --- src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java | 3 +++ src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java | 5 +++++ src/forkjoin/scala/concurrent/forkjoin/ForkJoinWorkerThread.java | 1 + src/forkjoin/scala/concurrent/forkjoin/LinkedTransferQueue.java | 3 +++ src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java | 1 + src/forkjoin/scala/concurrent/forkjoin/RecursiveTask.java | 1 + src/forkjoin/scala/concurrent/forkjoin/ThreadLocalRandom.java | 2 ++ src/forkjoin/scala/concurrent/forkjoin/TransferQueue.java | 1 + src/forkjoin/scala/concurrent/util/Unsafe.java | 6 ++---- 9 files changed, 19 insertions(+), 4 deletions(-) (limited to 'src/forkjoin') diff --git a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java index 6578504155..9bd378c61c 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java +++ b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; * @since 1.8 * @author Doug Lea */ +@Deprecated /*public*/ abstract class CountedCompleter extends ForkJoinTask { private static final long serialVersionUID = 5232453752276485070L; @@ -471,6 +472,7 @@ import java.util.concurrent.TimeUnit; * @since 1.7 * @author Doug Lea */ +@Deprecated public class ForkJoinPool extends AbstractExecutorService { /* @@ -3578,6 +3580,7 @@ public class ForkJoinPool extends AbstractExecutorService { * } * }} */ + @Deprecated public static interface ManagedBlocker { /** * Possibly blocks the current thread, for example waiting for diff --git a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java index fd1e132b07..b4f5c24ca9 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java +++ b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java @@ -180,6 +180,7 @@ import java.lang.reflect.Constructor; * @since 1.7 * @author Doug Lea */ +@Deprecated public abstract class ForkJoinTask implements Future, Serializable { /* @@ -391,6 +392,7 @@ public abstract class ForkJoinTask implements Future, Serializable { * any ForkJoinPool will call helpExpungeStaleExceptions when its * pool becomes isQuiescent. */ + @Deprecated static final class ExceptionNode extends WeakReference> { final Throwable ex; ExceptionNode next; @@ -1330,6 +1332,7 @@ public abstract class ForkJoinTask implements Future, Serializable { * to be compliant with AbstractExecutorService constraints * when used in ForkJoinPool. */ + @Deprecated static final class AdaptedRunnable extends ForkJoinTask implements RunnableFuture { final Runnable runnable; @@ -1349,6 +1352,7 @@ public abstract class ForkJoinTask implements Future, Serializable { /** * Adaptor for Runnables without results */ + @Deprecated static final class AdaptedRunnableAction extends ForkJoinTask implements RunnableFuture { final Runnable runnable; @@ -1366,6 +1370,7 @@ public abstract class ForkJoinTask implements Future, Serializable { /** * Adaptor for Callables */ + @Deprecated static final class AdaptedCallable extends ForkJoinTask implements RunnableFuture { final Callable callable; diff --git a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinWorkerThread.java b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinWorkerThread.java index e62fc6eb71..e00fb5cc43 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinWorkerThread.java +++ b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinWorkerThread.java @@ -20,6 +20,7 @@ package scala.concurrent.forkjoin; * @since 1.7 * @author Doug Lea */ +@Deprecated public class ForkJoinWorkerThread extends Thread { /* * ForkJoinWorkerThreads are managed by ForkJoinPools and perform diff --git a/src/forkjoin/scala/concurrent/forkjoin/LinkedTransferQueue.java b/src/forkjoin/scala/concurrent/forkjoin/LinkedTransferQueue.java index 07e81b395d..47d52af895 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/LinkedTransferQueue.java +++ b/src/forkjoin/scala/concurrent/forkjoin/LinkedTransferQueue.java @@ -53,6 +53,7 @@ import java.util.concurrent.locks.LockSupport; * @author Doug Lea * @param the type of elements held in this collection */ +@Deprecated public class LinkedTransferQueue extends AbstractQueue implements TransferQueue, java.io.Serializable { private static final long serialVersionUID = -3223113410248163686L; @@ -416,6 +417,7 @@ public class LinkedTransferQueue extends AbstractQueue * unnecessary ordering constraints: Writes that are intrinsically * ordered wrt other accesses or CASes use simple relaxed forms. */ + @Deprecated static final class Node { final boolean isData; // false if this is a request node volatile Object item; // initially non-null if isData; CASed to match @@ -789,6 +791,7 @@ public class LinkedTransferQueue extends AbstractQueue return count; } + @Deprecated final class Itr implements Iterator { private Node nextNode; // next node to return item for private E nextItem; // the corresponding item diff --git a/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java b/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java index 1e7cdd952d..f4a77f0f61 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java +++ b/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java @@ -133,6 +133,7 @@ package scala.concurrent.forkjoin; * @since 1.7 * @author Doug Lea */ +@Deprecated public abstract class RecursiveAction extends ForkJoinTask { private static final long serialVersionUID = 5232453952276485070L; diff --git a/src/forkjoin/scala/concurrent/forkjoin/RecursiveTask.java b/src/forkjoin/scala/concurrent/forkjoin/RecursiveTask.java index d1e1547143..097b7cda1f 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/RecursiveTask.java +++ b/src/forkjoin/scala/concurrent/forkjoin/RecursiveTask.java @@ -36,6 +36,7 @@ package scala.concurrent.forkjoin; * @since 1.7 * @author Doug Lea */ +@Deprecated public abstract class RecursiveTask extends ForkJoinTask { private static final long serialVersionUID = 5232453952276485270L; diff --git a/src/forkjoin/scala/concurrent/forkjoin/ThreadLocalRandom.java b/src/forkjoin/scala/concurrent/forkjoin/ThreadLocalRandom.java index 19237c9092..3ea1af66bc 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/ThreadLocalRandom.java +++ b/src/forkjoin/scala/concurrent/forkjoin/ThreadLocalRandom.java @@ -32,6 +32,7 @@ import java.util.Random; * @since 1.7 * @author Doug Lea */ +@Deprecated public class ThreadLocalRandom extends Random { // same constants as Random, but must be redeclared because private private static final long multiplier = 0x5DEECE66DL; @@ -80,6 +81,7 @@ public class ThreadLocalRandom extends Random { * * @return the current thread's {@code ThreadLocalRandom} */ + @Deprecated public static ThreadLocalRandom current() { return localRandom.get(); } diff --git a/src/forkjoin/scala/concurrent/forkjoin/TransferQueue.java b/src/forkjoin/scala/concurrent/forkjoin/TransferQueue.java index 7d149c7ae5..4fcd8ea601 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/TransferQueue.java +++ b/src/forkjoin/scala/concurrent/forkjoin/TransferQueue.java @@ -37,6 +37,7 @@ import java.util.concurrent.*; * @author Doug Lea * @param the type of elements held in this collection */ +@Deprecated public interface TransferQueue extends BlockingQueue { /** * Transfers the element to a waiting consumer immediately, if possible. diff --git a/src/forkjoin/scala/concurrent/util/Unsafe.java b/src/forkjoin/scala/concurrent/util/Unsafe.java index ef893c94d9..d82e4bbdd5 100644 --- a/src/forkjoin/scala/concurrent/util/Unsafe.java +++ b/src/forkjoin/scala/concurrent/util/Unsafe.java @@ -7,14 +7,12 @@ \* */ package scala.concurrent.util; - - - import java.lang.reflect.Field; - +@Deprecated public final class Unsafe { + @Deprecated public final static sun.misc.Unsafe instance; static { try { -- cgit v1.2.3