Class ThreadUtil
java.lang.Object
com.pnfsoftware.jeb.util.concurrent.ThreadUtil
Utility methods to manipulate threads.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Thread
Create a daemon thread.static String
Convenience method to generate the simplified stack trace of the caller, on a single line.static String
formatStackTrace
(int skipcnt) Convenience method to generate the simplified stack trace of the caller, on a single line.static String
formatStackTrace
(int skipcnt, String separator, boolean displayClassPackageName, boolean displayLineNumber) Convenience method to generate the simplified stack trace of the caller.static void
Interrupt the target thread and wait for it to terminate, uninterruptibly.static void
Join a thread uninterruptibly.static boolean
monitor
(Thread t, IMonitorInfoProvider provider) Monitor a running thread, and attempt to terminate it after a dynamic timeout has elapsed.static Thread
Create and start a new daemon thread.static Thread
Create and start a new daemon thread.static boolean
Gracefully stop a running thread.
-
Constructor Details
-
ThreadUtil
public ThreadUtil()
-
-
Method Details
-
create
Create a daemon thread. The thread is not started.- Parameters:
r
- the runnable- Returns:
- the new thread
-
start
Create and start a new daemon thread.- Parameters:
r
- the runnable- Returns:
- the running thread
-
start
Create and start a new daemon thread.- Parameters:
name
- thread namer
- the runnable- Returns:
- the running thread
-
stop
Gracefully stop a running thread. This method is blocking.- Parameters:
t
- the running thread- Returns:
- true if the thread was stopped successfully
-
monitor
Monitor a running thread, and attempt to terminate it after a dynamic timeout has elapsed. This method is blocking. In order for this method to work properly and responsively, the monitored thread should check forinterruptions
regularly, and should not disregardInterruptedException
s.- Parameters:
t
- the running threadprovider
- information for the monitor (timeout, probingPeriod, etc.); if the probing period is invalid, it will be set to a default 100ms.- Returns:
- true if the thread has ended successfully (may have ended because it completed, may have ended because it processed the interruption); unless something bad happens (eg, the monitor is interrupted), this method should always return true
-
joinU
Join a thread uninterruptibly.Note: after successfully joining, if an
InterruptedException
had arisen, theinterrupt
flag is re-armed.- Parameters:
thread
- the thread to join on
-
interruptAndJoinUninterruptibly
Interrupt the target thread and wait for it to terminate, uninterruptibly.- Parameters:
thread
- target Thread
-
formatStackTrace
Convenience method to generate the simplified stack trace of the caller, on a single line.- Returns:
- the formatted stack trace
-
formatStackTrace
Convenience method to generate the simplified stack trace of the caller, on a single line.- Parameters:
skipcnt
- how many top-level callers should be removed- Returns:
- the formatted stack trace
-
formatStackTrace
public static String formatStackTrace(int skipcnt, String separator, boolean displayClassPackageName, boolean displayLineNumber) Convenience method to generate the simplified stack trace of the caller.- Parameters:
skipcnt
- how many top-level callers should be removedseparator
- optional separator, use null for default (" < "
)displayClassPackageName
-displayLineNumber
-- Returns:
- the formatted stack trace
-