Package com.alibaba.ttl
Class TtlRunnable
java.lang.Object
com.alibaba.ttl.TtlRunnable
- All Implemented Interfaces:
TtlAttachments,TtlEnhanced,TtlWrapper<Runnable>,Runnable
public final class TtlRunnable
extends Object
implements Runnable, TtlWrapper<Runnable>, TtlEnhanced, TtlAttachments
TtlRunnable decorate Runnable to get TransmittableThreadLocal value
and transmit it to the time of Runnable execution, needed when use Runnable to thread pool.
Use factory methods get(java.lang.Runnable) / gets(java.util.Collection<? extends java.lang.Runnable>) to create instance.
Other TTL Wrapper for common Functional Interface see TtlWrappers.
- Since:
- 0.9.0
- Author:
- Jerry Lee (oldratlee at gmail dot com)
- See Also:
-
Field Summary
Fields inherited from interface com.alibaba.ttl.spi.TtlAttachments
KEY_IS_AUTO_WRAPPER -
Method Summary
Modifier and TypeMethodDescriptionbooleanstatic TtlRunnableFactory method, wrap inputRunnabletoTtlRunnable.static TtlRunnableFactory method, wrap inputRunnabletoTtlRunnable.static TtlRunnableFactory method, wrap inputRunnabletoTtlRunnable.return original/unwrappedRunnable.static List<TtlRunnable>gets(Collection<? extends Runnable> tasks) wrap inputRunnableCollection toTtlRunnableCollection.static List<TtlRunnable>gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) wrap inputRunnableCollection toTtlRunnableCollection.static List<TtlRunnable>gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) wrap inputRunnableCollection toTtlRunnableCollection.<T> TgetTtlAttachment(String key) inthashCode()voidrun()wrap methodRunnable.run().voidsetTtlAttachment(String key, Object value) toString()unwrap()unwrap to original/unwrappedRunnable.static RunnableUnwrapTtlRunnableto the original/underneath one.unwraps(Collection<? extends Runnable> tasks) UnwrapTtlRunnableto the original/underneath one for collection.
-
Method Details
-
run
public void run()wrap methodRunnable.run(). -
getRunnable
return original/unwrappedRunnable. -
unwrap
unwrap to original/unwrappedRunnable.- Specified by:
unwrapin interfaceTtlWrapper<Runnable>- Since:
- 2.11.4
- See Also:
-
equals
-
hashCode
public int hashCode() -
toString
-
get
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static TtlRunnable get(@Nullable Runnable runnable) Factory method, wrap inputRunnabletoTtlRunnable.- Parameters:
runnable- inputRunnable. if input isnull, returnnull.- Returns:
- Wrapped
Runnable - Throws:
IllegalStateException- when input isTtlRunnablealready.
-
get
@Nullable @Contract(value="null, _ -> null; !null, _ -> !null", pure=true) public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun) Factory method, wrap inputRunnabletoTtlRunnable.- Parameters:
runnable- inputRunnable. if input isnull, returnnull.releaseTtlValueReferenceAfterRun- release TTL value reference after run, avoid memory leak even ifTtlRunnableis referred.- Returns:
- Wrapped
Runnable - Throws:
IllegalStateException- when input isTtlRunnablealready.
-
get
@Nullable @Contract(value="null, _, _ -> null; !null, _, _ -> !null", pure=true) public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) Factory method, wrap inputRunnabletoTtlRunnable.- Parameters:
runnable- inputRunnable. if input isnull, returnnull.releaseTtlValueReferenceAfterRun- release TTL value reference after run, avoid memory leak even ifTtlRunnableis referred.idempotent- is idempotent mode or not. iftrue, just return inputRunnablewhen it'sTtlRunnable, otherwise throwIllegalStateException. Caution:truewill cover up bugs! DO NOT set, only when you know why.- Returns:
- Wrapped
Runnable - Throws:
IllegalStateException- when input isTtlRunnablealready and not idempotent.
-
gets
wrap inputRunnableCollection toTtlRunnableCollection.- Parameters:
tasks- task to be wrapped. if input isnull, returnnull.- Returns:
- wrapped tasks
- Throws:
IllegalStateException- when input isTtlRunnablealready.
-
gets
@NonNull public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun) wrap inputRunnableCollection toTtlRunnableCollection.- Parameters:
tasks- task to be wrapped. if input isnull, returnnull.releaseTtlValueReferenceAfterRun- release TTL value reference after run, avoid memory leak even ifTtlRunnableis referred.- Returns:
- wrapped tasks
- Throws:
IllegalStateException- when input isTtlRunnablealready.
-
gets
@NonNull public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent) wrap inputRunnableCollection toTtlRunnableCollection.- Parameters:
tasks- task to be wrapped. if input isnull, returnnull.releaseTtlValueReferenceAfterRun- release TTL value reference after run, avoid memory leak even ifTtlRunnableis referred.idempotent- is idempotent mode or not. iftrue, just return inputRunnablewhen it'sTtlRunnable, otherwise throwIllegalStateException. Caution:truewill cover up bugs! DO NOT set, only when you know why.- Returns:
- wrapped tasks
- Throws:
IllegalStateException- when input isTtlRunnablealready and not idempotent.
-
unwrap
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static Runnable unwrap(@Nullable Runnable runnable) UnwrapTtlRunnableto the original/underneath one.this method is
null-safe, when inputRunnableparameter isnull, returnnull; if inputRunnableparameter is not aTtlRunnablejust return inputRunnable.so
TtlRunnable.unwrap(TtlRunnable.get(runnable))will always return the same inputrunnableobject.- Since:
- 2.10.2
- See Also:
-
unwraps
UnwrapTtlRunnableto the original/underneath one for collection.Invoke
unwrap(Runnable)for each element in input collection.This method is
null-safe, when inputRunnableparameter collection isnull, return a empty list.- Since:
- 2.10.2
- See Also:
-
setTtlAttachment
- Specified by:
setTtlAttachmentin interfaceTtlAttachments- Parameters:
key- attachment keyvalue- attachment value- Since:
- 2.11.0
-
getTtlAttachment
- Specified by:
getTtlAttachmentin interfaceTtlAttachments- Parameters:
key- attachment key- Since:
- 2.11.0
-