diff --git a/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java b/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java index 789b795f8aaa0bfbf85e37d469d9c6da2e646445..fd0b1d1ec14ff343f12f4cad3c9bcab9a18577ae 100644 --- a/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java +++ b/src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java @@ -21,21 +21,23 @@ import java.util.logging.Logger; * If the inheritable ability from {@link InheritableThreadLocal} has potential leaking problem, * you can disable the inheritable ability: *

- * ❶ by wrapping thread factory using method + * ❶ For thread pooling components({@link java.util.concurrent.ThreadPoolExecutor}, + * {@link java.util.concurrent.ForkJoinPool}), Inheritable feature should never happen, + * since threads in thread pooling components is pre-created and pooled, these threads is neutral to biz logic/data. + *
+ * Disable inheritable for thread pooling components by wrapping thread factories using method * {@link com.alibaba.ttl.threadpool.TtlExecutors#getDisableInheritableThreadFactory(java.util.concurrent.ThreadFactory)} / - * {@link com.alibaba.ttl.threadpool.TtlForkJoinPoolHelper#getDefaultDisableInheritableForkJoinWorkerThreadFactory()} - * for thread pooling components({@link java.util.concurrent.ThreadPoolExecutor}, {@link java.util.concurrent.ForkJoinPool}). - * Inheritable feature in thread pooling components should never happen, - * because threads in thread pooling components is pre-created and pooled, these threads is neutral for biz logic/data. + * {@link com.alibaba.ttl.threadpool.TtlForkJoinPoolHelper#getDefaultDisableInheritableForkJoinWorkerThreadFactory()}. *
- * You can turn on "disable inheritable for thread pool" by {@link com.alibaba.ttl.threadpool.agent.TtlAgent} - * so as to wrap thread factory for thread pooling components ({@link java.util.concurrent.ThreadPoolExecutor}, - * {@link java.util.concurrent.ForkJoinPool}) automatically and transparently. + * Or you can turn on "disable inheritable for thread pool" by {@link com.alibaba.ttl.threadpool.agent.TtlAgent} + * so as to wrap thread factories for thread pooling components automatically and transparently. *

- * ❷ or by overriding method {@link #childValue(Object)}. + * ❷ In other cases, disable inheritable by overriding method {@link #childValue(Object)}. + *
* Whether the value should be inheritable or not can be controlled by the data owner, * disable it carefully when data owner have a clear idea. - *

 {@code TransmittableThreadLocal transmittableThreadLocal = new TransmittableThreadLocal() {
+ * 
{@code
+ * TransmittableThreadLocal transmittableThreadLocal = new TransmittableThreadLocal<>() {
  *     protected String childValue(String parentValue) {
  *         return initialValue();
  *     }
@@ -67,9 +69,8 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple
     private final boolean disableIgnoreNullValueSemantics;
 
     /**
-     * Default constructor.
+     * Default constructor. Create a {@link TransmittableThreadLocal} instance with "Ignore-Null-Value Semantics".
      * 

- * Create a {@link TransmittableThreadLocal} instance with "Ignore-Null-Value Semantics". * About "Ignore-Null-Value Semantics": *

*

    @@ -79,18 +80,19 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple *

    * This is a pragmatic design decision: *

      - *
    1. use explicit value type rather than {@code null} to biz intent.
    2. - *
    3. more safe(avoid {@code NPE}) and gc friendly.
    4. + *
    5. use explicit value type rather than {@code null} value to express biz intent.
    6. + *
    7. safer and more robust code(avoid {@code NPE} risk).
    8. *
    *

    - * So it's not recommended to use {@code null} value. + * So it's strongly not recommended to use {@code null} value. *

    * But the behavior of "Ignore-Null-Value Semantics" is NOT compatible with * {@link ThreadLocal} and {@link InheritableThreadLocal}, * you can disable this behavior/semantics via using constructor {@link #TransmittableThreadLocal(boolean)} - * and set parameter {@code disableIgnoreNullValueSemantics} instead. + * and setting parameter {@code disableIgnoreNullValueSemantics} to {@code true}. *

    - * More info see Issue #157. + * More discussion about "Ignore-Null-Value Semantics" see + * Issue #157. * * @see #TransmittableThreadLocal(boolean) */ @@ -99,8 +101,8 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple } /** - * Constructor, create a {@link TransmittableThreadLocal} with parameter {@code disableIgnoreNullValueSemantics} - * to control "Ignore-Null-Value Semantics". + * Constructor, create a {@link TransmittableThreadLocal} instance + * with parameter {@code disableIgnoreNullValueSemantics} to control "Ignore-Null-Value Semantics". * * @param disableIgnoreNullValueSemantics disable "Ignore-Null-Value Semantics" * @see #TransmittableThreadLocal() @@ -272,7 +274,7 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple *

    Framework/Middleware integration to TTL transmittance

    * Below is the example code: * - *
    
    +     * 
    {@code
          * ///////////////////////////////////////////////////////////////////////////
          * // in thread A, capture all TransmittableThreadLocal values of thread A
          * ///////////////////////////////////////////////////////////////////////////
    @@ -293,8 +295,7 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple
          * } finally {
          *     // restore the TransmittableThreadLocal of thread B when replay
          *     Transmitter.restore(backup); (3)
    -     * }
    -     * 
    + * }}
    *

    * see the implementation code of {@link TtlRunnable} and {@link TtlCallable} for more actual code sample. *

    @@ -304,7 +305,7 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple *

    * Below is the example code: * - *

    
    +     * 
    {@code
          * ///////////////////////////////////////////////////////////////////////////
          * // in thread A, capture all TransmittableThreadLocal values of thread A
          * ///////////////////////////////////////////////////////////////////////////
    @@ -320,8 +321,7 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple
          *      System.out.println("Hello");
          *      ...
          *      return "World";
    -     * }); // (2) + (3)
    -     * 
    + * }); // (2) + (3)}
    *

    * The reason of providing 2 util methods is the different {@code throws Exception} type * so as to satisfy your biz logic({@code lambda}): @@ -331,7 +331,8 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple *

*

* If you need the different {@code throws Exception} type, - * you can define your own util method(function interface({@code lambda})) with your own {@code throws Exception} type. + * you can define your own util method(function interface({@code lambda})) + * with your own {@code throws Exception} type. * *

ThreadLocal Integration

* If you can not rewrite the existed code which use {@link ThreadLocal} to {@link TransmittableThreadLocal}, @@ -341,13 +342,12 @@ public class TransmittableThreadLocal extends InheritableThreadLocal imple *

* Below is the example code: * - *


+     * 
{@code
      * // the value of this ThreadLocal instance will be transmitted after registered
      * Transmitter.registerThreadLocal(aThreadLocal, copyLambda);
      *
      * // Then the value of this ThreadLocal instance will not be transmitted after unregistered
-     * Transmitter.unregisterThreadLocal(aThreadLocal);
-     * 
+ * Transmitter.unregisterThreadLocal(aThreadLocal);}
* * Caution:
* If the registered {@link ThreadLocal} instance is not {@link InheritableThreadLocal}, diff --git a/src/main/java/com/alibaba/ttl/spi/TtlAttachments.java b/src/main/java/com/alibaba/ttl/spi/TtlAttachments.java index b06cbb625a0e6e4fee60f08289e553542806a849..9185805ad322856aa7af4d321bda7795f0630e0e 100644 --- a/src/main/java/com/alibaba/ttl/spi/TtlAttachments.java +++ b/src/main/java/com/alibaba/ttl/spi/TtlAttachments.java @@ -3,7 +3,8 @@ package com.alibaba.ttl.spi; import edu.umd.cs.findbugs.annotations.NonNull; /** - * The TTL attachments for TTL tasks, eg: {@link com.alibaba.ttl.TtlRunnable}, {@link com.alibaba.ttl.TtlCallable}. + * The TTL attachments for TTL tasks, + * eg: {@link com.alibaba.ttl.TtlRunnable}, {@link com.alibaba.ttl.TtlCallable}. * * @author Jerry Lee (oldratlee at gmail dot com) * @since 2.11.0 diff --git a/src/main/java/com/alibaba/ttl/spi/TtlEnhanced.java b/src/main/java/com/alibaba/ttl/spi/TtlEnhanced.java index 8d719cf0a28f01c355269f60d28fccfcbba20c84..19dcd2d82d43967e2e1a6fee51ea0f9aae102e10 100644 --- a/src/main/java/com/alibaba/ttl/spi/TtlEnhanced.java +++ b/src/main/java/com/alibaba/ttl/spi/TtlEnhanced.java @@ -1,7 +1,8 @@ package com.alibaba.ttl.spi; /** - * a Ttl marker/tag interface, for ttl enhanced class, for example {@code TTL wrapper} like {@link com.alibaba.ttl.TtlRunnable}, {@link com.alibaba.ttl.TtlCallable}. + * a Ttl marker/tag interface, for ttl enhanced class, for example {@code TTL wrapper} + * like {@link com.alibaba.ttl.TtlRunnable}, {@link com.alibaba.ttl.TtlCallable}. * * @author Jerry Lee (oldratlee at gmail dot com) * @see com.alibaba.ttl.TtlRunnable diff --git a/src/test/java/com/alibaba/ttl/TtlWrappersTest.kt b/src/test/java/com/alibaba/ttl/TtlWrappersTest.kt index 2a6a83884b9e5f50fd1d76ff7a6a8c20706525b9..e17da12e95d57b14dbf20b5e4034d9433a87ccd3 100644 --- a/src/test/java/com/alibaba/ttl/TtlWrappersTest.kt +++ b/src/test/java/com/alibaba/ttl/TtlWrappersTest.kt @@ -243,7 +243,7 @@ class TtlWrappersTest { fun afterClass() { executorService.shutdown() executorService.awaitTermination(100, TimeUnit.MILLISECONDS) - if (!executorService.isTerminated) Assert.fail("Fail to shutdown thread pool") + if (!executorService.isTerminated) fail("Fail to shutdown thread pool") } } }