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
TtlRunnabledecorateRunnable, so as to getTransmittableThreadLocaland transmit it to the time ofRunnableexecution, needed when useRunnableto 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 InterfaceseeTtlWrappers.- Since:
- 0.9.0
- Author:
- Jerry Lee (oldratlee at gmail dot com)
- See Also:
TtlExecutors,TtlWrappers,Executor,ExecutorService,ThreadPoolExecutor,ScheduledThreadPoolExecutor,Executors
-
-
Field Summary
-
Fields inherited from interface com.alibaba.ttl.spi.TtlAttachments
KEY_IS_AUTO_WRAPPER
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)static TtlRunnableget(Runnable runnable)Factory method, wrap inputRunnabletoTtlRunnable.static TtlRunnableget(Runnable runnable, boolean releaseTtlValueReferenceAfterRun)Factory method, wrap inputRunnabletoTtlRunnable.static TtlRunnableget(Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent)Factory method, wrap inputRunnabletoTtlRunnable.RunnablegetRunnable()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)StringtoString()Runnableunwrap()unwrap to original/unwrappedRunnable.static Runnableunwrap(Runnable runnable)UnwrapTtlRunnableto the original/underneath one.static List<Runnable>unwraps(Collection<? extends Runnable> tasks)UnwrapTtlRunnableto the original/underneath one for collection.
-
-
-
Method Detail
-
run
public void run()
wrap methodRunnable.run().
-
unwrap
@NonNull public Runnable unwrap()
unwrap to original/unwrappedRunnable.- Specified by:
unwrapin interfaceTtlWrapper<Runnable>- Since:
- 2.11.4
- See Also:
TtlUnwrap.unwrap(Object)
-
get
@Nullable 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 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 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
@NonNull public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks)
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 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:
get(Runnable)
-
unwraps
@NonNull public static List<Runnable> unwraps(@Nullable Collection<? extends Runnable> tasks)
UnwrapTtlRunnableto the original/underneath one for collection.Invoke
unwrap(Runnable)for each element in input collection.This method is
null-safe, when inputRunnableparameter isnull, return a empty list.- Since:
- 2.10.2
- See Also:
gets(Collection),unwrap(Runnable)
-
setTtlAttachment
public void setTtlAttachment(@NonNull String key, Object value)
- Specified by:
setTtlAttachmentin interfaceTtlAttachments- Parameters:
key- attachment keyvalue- attachment value- Since:
- 2.11.0
-
getTtlAttachment
public <T> T getTtlAttachment(@NonNull String key)
- Specified by:
getTtlAttachmentin interfaceTtlAttachments- Parameters:
key- attachment key- Since:
- 2.11.0
-
-