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
decorateRunnable
, so as to getTransmittableThreadLocal
and transmit it to the time ofRunnable
execution, needed when useRunnable
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
seeTtlWrappers
.- 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 boolean
equals(Object o)
static TtlRunnable
get(Runnable runnable)
Factory method, wrap inputRunnable
toTtlRunnable
.static TtlRunnable
get(Runnable runnable, boolean releaseTtlValueReferenceAfterRun)
Factory method, wrap inputRunnable
toTtlRunnable
.static TtlRunnable
get(Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent)
Factory method, wrap inputRunnable
toTtlRunnable
.Runnable
getRunnable()
return original/unwrappedRunnable
.static List<TtlRunnable>
gets(Collection<? extends Runnable> tasks)
wrap inputRunnable
Collection toTtlRunnable
Collection.static List<TtlRunnable>
gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun)
wrap inputRunnable
Collection toTtlRunnable
Collection.static List<TtlRunnable>
gets(Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent)
wrap inputRunnable
Collection toTtlRunnable
Collection.<T> T
getTtlAttachment(String key)
int
hashCode()
void
run()
wrap methodRunnable.run()
.void
setTtlAttachment(String key, Object value)
String
toString()
Runnable
unwrap()
unwrap to original/unwrappedRunnable
.static Runnable
unwrap(Runnable runnable)
UnwrapTtlRunnable
to the original/underneath one.static List<Runnable>
unwraps(Collection<? extends Runnable> tasks)
UnwrapTtlRunnable
to 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:
unwrap
in interfaceTtlWrapper<Runnable>
- Since:
- 2.11.4
- See Also:
TtlUnwrap.unwrap(Object)
-
get
@Nullable public static TtlRunnable get(@Nullable Runnable runnable)
Factory method, wrap inputRunnable
toTtlRunnable
.- Parameters:
runnable
- inputRunnable
. if input isnull
, returnnull
.- Returns:
- Wrapped
Runnable
- Throws:
IllegalStateException
- when input isTtlRunnable
already.
-
get
@Nullable public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun)
Factory method, wrap inputRunnable
toTtlRunnable
.- Parameters:
runnable
- inputRunnable
. if input isnull
, returnnull
.releaseTtlValueReferenceAfterRun
- release TTL value reference after run, avoid memory leak even ifTtlRunnable
is referred.- Returns:
- Wrapped
Runnable
- Throws:
IllegalStateException
- when input isTtlRunnable
already.
-
get
@Nullable public static TtlRunnable get(@Nullable Runnable runnable, boolean releaseTtlValueReferenceAfterRun, boolean idempotent)
Factory method, wrap inputRunnable
toTtlRunnable
.- Parameters:
runnable
- inputRunnable
. if input isnull
, returnnull
.releaseTtlValueReferenceAfterRun
- release TTL value reference after run, avoid memory leak even ifTtlRunnable
is referred.idempotent
- is idempotent mode or not. iftrue
, just return inputRunnable
when it'sTtlRunnable
, otherwise throwIllegalStateException
. Caution:true
will cover up bugs! DO NOT set, only when you know why.- Returns:
- Wrapped
Runnable
- Throws:
IllegalStateException
- when input isTtlRunnable
already and not idempotent.
-
gets
@NonNull public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks)
wrap inputRunnable
Collection toTtlRunnable
Collection.- Parameters:
tasks
- task to be wrapped. if input isnull
, returnnull
.- Returns:
- wrapped tasks
- Throws:
IllegalStateException
- when input isTtlRunnable
already.
-
gets
@NonNull public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun)
wrap inputRunnable
Collection toTtlRunnable
Collection.- Parameters:
tasks
- task to be wrapped. if input isnull
, returnnull
.releaseTtlValueReferenceAfterRun
- release TTL value reference after run, avoid memory leak even ifTtlRunnable
is referred.- Returns:
- wrapped tasks
- Throws:
IllegalStateException
- when input isTtlRunnable
already.
-
gets
@NonNull public static List<TtlRunnable> gets(@Nullable Collection<? extends Runnable> tasks, boolean releaseTtlValueReferenceAfterRun, boolean idempotent)
wrap inputRunnable
Collection toTtlRunnable
Collection.- Parameters:
tasks
- task to be wrapped. if input isnull
, returnnull
.releaseTtlValueReferenceAfterRun
- release TTL value reference after run, avoid memory leak even ifTtlRunnable
is referred.idempotent
- is idempotent mode or not. iftrue
, just return inputRunnable
when it'sTtlRunnable
, otherwise throwIllegalStateException
. Caution:true
will cover up bugs! DO NOT set, only when you know why.- Returns:
- wrapped tasks
- Throws:
IllegalStateException
- when input isTtlRunnable
already and not idempotent.
-
unwrap
@Nullable public static Runnable unwrap(@Nullable Runnable runnable)
UnwrapTtlRunnable
to the original/underneath one.this method is
null
-safe, when inputRunnable
parameter isnull
, returnnull
; if inputRunnable
parameter is not aTtlRunnable
just return inputRunnable
.so
TtlRunnable.unwrap(TtlRunnable.get(runnable))
will always return the same inputrunnable
object.- Since:
- 2.10.2
- See Also:
get(Runnable)
,TtlUnwrap.unwrap(Object)
-
unwraps
@NonNull public static List<Runnable> unwraps(@Nullable Collection<? extends Runnable> tasks)
UnwrapTtlRunnable
to the original/underneath one for collection.Invoke
unwrap(Runnable)
for each element in input collection.This method is
null
-safe, when inputRunnable
parameter collection 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:
setTtlAttachment
in interfaceTtlAttachments
- Parameters:
key
- attachment keyvalue
- attachment value- Since:
- 2.11.0
-
getTtlAttachment
public <T> T getTtlAttachment(@NonNull String key)
- Specified by:
getTtlAttachment
in interfaceTtlAttachments
- Parameters:
key
- attachment key- Since:
- 2.11.0
-
-