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 TypeMethodDescriptionboolean
static TtlRunnable
Factory method, wrap inputRunnable
toTtlRunnable
.static TtlRunnable
Factory method, wrap inputRunnable
toTtlRunnable
.static TtlRunnable
Factory method, wrap inputRunnable
toTtlRunnable
.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) toString()
unwrap()
unwrap to original/unwrappedRunnable
.static Runnable
UnwrapTtlRunnable
to the original/underneath one.unwraps
(Collection<? extends Runnable> tasks) UnwrapTtlRunnable
to 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:
unwrap
in 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 inputRunnable
toTtlRunnable
.- Parameters:
runnable
- inputRunnable
. if input isnull
, returnnull
.- Returns:
- Wrapped
Runnable
- Throws:
IllegalStateException
- when input isTtlRunnable
already.
-
get
@Nullable @Contract(value="null, _ -> null; !null, _ -> !null", pure=true) 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 @Contract(value="null, _, _ -> null; !null, _, _ -> !null", pure=true) 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
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 @Contract(value="null -> null; !null -> !null", pure=true) 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:
-
unwraps
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:
-
setTtlAttachment
- Specified by:
setTtlAttachment
in interfaceTtlAttachments
- Parameters:
key
- attachment keyvalue
- attachment value- Since:
- 2.11.0
-
getTtlAttachment
- Specified by:
getTtlAttachment
in interfaceTtlAttachments
- Parameters:
key
- attachment key- Since:
- 2.11.0
-