Package io.debezium.util
Class Threads
- java.lang.Object
-
- io.debezium.util.Threads
-
public class Threads extends Object
Utilities related to threads and threading.- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Threads.Timer
Expires after defined time period.static interface
Threads.TimeSince
Measures the amount time that has elapsed since the lastreset
.
-
Field Summary
Fields Modifier and Type Field Description private static String
DEBEZIUM_THREAD_NAME_PREFIX
private static org.slf4j.Logger
LOGGER
-
Constructor Summary
Constructors Modifier Constructor Description private
Threads()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Thread
interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, Threads.TimeSince elapsedTimer)
Create a thread that will interrupt the calling thread when theelapsed time
has exceeded the specified amount.static Thread
interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, Threads.TimeSince elapsedTimer, Thread threadToInterrupt)
Create a thread that will interrupt the given thread when theelapsed time
has exceeded the specified amount.static ExecutorService
newFixedThreadPool(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, int threadCount)
static ExecutorService
newSingleThreadExecutor(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name)
static ExecutorService
newSingleThreadExecutor(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, boolean daemon)
static ThreadFactory
threadFactory(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, boolean indexed, boolean daemon)
Returns a thread factory that creates threads conforming to Debezium thread naming patterndebezium-<connector class>-<connector-id>-<thread-name>
.static ThreadFactory
threadFactory(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, boolean indexed, boolean daemon, Consumer<Thread> callback)
Returns a thread factory that creates threads conforming to Debezium thread naming patterndebezium-<connector class>-<connector-id>-<thread-name>
.static Thread
timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, Threads.TimeSince elapsedTimer, Runnable uponTimeout)
Create a thread that will call the supplied function when theelapsed time
has exceeded the specified amount.static Thread
timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, LongSupplier elapsedTime, Runnable uponStart, Runnable uponTimeout)
Create a thread that will call the supplied function when the elapsed time has exceeded the specified amount.static Thread
timeout(String threadName, long timeout, TimeUnit timeoutUnit, Threads.TimeSince elapsedTimer, Runnable uponTimeout)
Create a thread that will call the supplied function when theelapsed time
has exceeded the specified amount.static Threads.Timer
timer(Clock clock, Duration time)
Obtain aThreads.Timer
that uses the given clock to indicate that a pre-defined time period expired.static Threads.TimeSince
timeSince(Clock clock)
Obtain aThreads.TimeSince
that uses the given clock to record the time elapsed.
-
-
-
Field Detail
-
DEBEZIUM_THREAD_NAME_PREFIX
private static final String DEBEZIUM_THREAD_NAME_PREFIX
- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
-
Method Detail
-
timeSince
public static Threads.TimeSince timeSince(Clock clock)
Obtain aThreads.TimeSince
that uses the given clock to record the time elapsed.- Parameters:
clock
- the clock; may not be null- Returns:
- the
Threads.TimeSince
object; never null
-
timer
public static Threads.Timer timer(Clock clock, Duration time)
Obtain aThreads.Timer
that uses the given clock to indicate that a pre-defined time period expired.- Parameters:
clock
- the clock; may not be nulltime
- a time interval to expire- Returns:
- the
Threads.Timer
object; never null
-
interruptAfterTimeout
public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, Threads.TimeSince elapsedTimer)
Create a thread that will interrupt the calling thread when theelapsed time
has exceeded the specified amount. The suppliedThreads.TimeSince
object will bereset
when the new thread is started, and should also bereset
any time the elapsed time should be reset to 0.- Parameters:
threadName
- the name of the new thread; may not be nulltimeout
- the maximum amount of time that can elapse before the thread is interrupted; must be positivetimeoutUnit
- the unit fortimeout
; may not be nullelapsedTimer
- the component used to measure the elapsed time; may not be null- Returns:
- the new thread that has not yet been
started
; never null
-
interruptAfterTimeout
public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, Threads.TimeSince elapsedTimer, Thread threadToInterrupt)
Create a thread that will interrupt the given thread when theelapsed time
has exceeded the specified amount. The suppliedThreads.TimeSince
object will bereset
when the new thread is started, and should also bereset
any time the elapsed time should be reset to 0.- Parameters:
threadName
- the name of the new thread; may not be nulltimeout
- the maximum amount of time that can elapse before the thread is interrupted; must be positivetimeoutUnit
- the unit fortimeout
; may not be nullelapsedTimer
- the component used to measure the elapsed time; may not be nullthreadToInterrupt
- the thread that should be interrupted upon timeout; may not be null- Returns:
- the new thread that has not yet been
started
; never null
-
timeout
public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, Threads.TimeSince elapsedTimer, Runnable uponTimeout)
Create a thread that will call the supplied function when theelapsed time
has exceeded the specified amount. The suppliedThreads.TimeSince
object will bereset
when the new thread is started, and should also bereset
any time the elapsed time should be reset to 0.The thread checks the elapsed time every 100 milliseconds.
- Parameters:
threadName
- the name of the new thread; may not be nulltimeout
- the maximum amount of time that can elapse before the thread is interrupted; must be positivetimeoutUnit
- the unit fortimeout
; may not be nullelapsedTimer
- the component used to measure the elapsed time; may not be nulluponTimeout
- the function to be called when the maximum amount of time has elapsed; may not be null- Returns:
- the new thread that has not yet been
started
; never null
-
timeout
public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, Threads.TimeSince elapsedTimer, Runnable uponTimeout)
Create a thread that will call the supplied function when theelapsed time
has exceeded the specified amount. The suppliedThreads.TimeSince
object will bereset
when the new thread is started, and should also bereset
any time the elapsed time should be reset to 0.The thread checks the elapsed time every 100 milliseconds.
- Parameters:
threadName
- the name of the new thread; may not be nulltimeout
- the maximum amount of time that can elapse before the thread is interrupted; must be positivetimeoutUnit
- the unit fortimeout
; may not be nullsleepInterval
- the amount of time for the new thread to sleep after checking the elapsed time; must be positivesleepUnit
- the unit forsleepInterval
; may not be nullelapsedTimer
- the component used to measure the elapsed time; may not be nulluponTimeout
- the function to be called when the maximum amount of time has elapsed; may not be null- Returns:
- the new thread that has not yet been
started
; never null
-
timeout
public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, LongSupplier elapsedTime, Runnable uponStart, Runnable uponTimeout)
Create a thread that will call the supplied function when the elapsed time has exceeded the specified amount.- Parameters:
threadName
- the name of the new thread; may not be nulltimeout
- the maximum amount of time that can elapse before the thread is interrupted; must be positivetimeoutUnit
- the unit fortimeout
; may not be nullsleepInterval
- the amount of time for the new thread to sleep after checking the elapsed time; must be positivesleepUnit
- the unit forsleepInterval
; may not be nullelapsedTime
- the function that returns the total elapsed time; may not be nulluponStart
- the function that will be called when the returned thread isstarted
; may be nulluponTimeout
- the function to be called when the maximum amount of time has elapsed; may not be null- Returns:
- the new thread that has not yet been
started
; never null
-
threadFactory
public static ThreadFactory threadFactory(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, boolean indexed, boolean daemon)
Returns a thread factory that creates threads conforming to Debezium thread naming patterndebezium-<connector class>-<connector-id>-<thread-name>
.- Parameters:
connector
- - the source connector classconnectorId
- - the identifier to differentiate between connector instancesname
- - the name of the threadindexed
- - true if the thread name should be appended with an indexdaemon
- - true if the thread should be a daemon thread- Returns:
- the thread factory setting the correct name
-
threadFactory
public static ThreadFactory threadFactory(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, boolean indexed, boolean daemon, Consumer<Thread> callback)
Returns a thread factory that creates threads conforming to Debezium thread naming patterndebezium-<connector class>-<connector-id>-<thread-name>
.- Parameters:
connector
- - the source connector classconnectorId
- - the identifier to differentiate between connector instancesname
- - the name of the threadindexed
- - true if the thread name should be appended with an indexdaemon
- - true if the thread should be a daemon threadcallback
- - a callback called on every thread created- Returns:
- the thread factory setting the correct name
-
newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, boolean daemon)
-
newFixedThreadPool
public static ExecutorService newFixedThreadPool(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name, int threadCount)
-
newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector, String connectorId, String name)
-
-