public class ThreadUtil extends Object
Constructor and Description |
---|
ThreadUtil() |
Modifier and Type | Method and Description |
---|---|
static ConcurrencyTester |
concurrencyTest(int threadSize,
Runnable runnable)
并发测试
此方法用于测试多线程下执行某些逻辑的并发性能 调用此方法会导致当前线程阻塞。 |
static ScheduledThreadPoolExecutor |
createScheduledExecutor(int corePoolSize)
|
static ThreadFactory |
createThreadFactory(String threadNamePrefix)
创建自定义线程名称前缀的
ThreadFactory |
static ThreadFactoryBuilder |
createThreadFactoryBuilder()
创建ThreadFactoryBuilder
|
static <T> ThreadLocal<T> |
createThreadLocal(boolean isInheritable)
创建本地线程对象
|
static <T> ThreadLocal<T> |
createThreadLocal(Supplier<? extends T> supplier)
创建本地线程对象
|
static ThreadGroup |
currentThreadGroup()
获取当前线程的线程组
|
static <T> Future<T> |
execAsync(Callable<T> task)
执行有返回值的异步方法
Future代表一个异步执行的操作,通过get()方法可以获得操作的结果,如果异步操作还没有完成,则,get()会使当前线程阻塞 |
static Future<?> |
execAsync(Runnable runnable)
执行有返回值的异步方法
Future代表一个异步执行的操作,通过get()方法可以获得操作的结果,如果异步操作还没有完成,则,get()会使当前线程阻塞 |
static Runnable |
execAsync(Runnable runnable,
boolean isDaemon)
执行异步方法
|
static void |
execute(Runnable runnable)
直接在公共线程池中执行线程
|
static Thread |
getMainThread()
获取进程的主线程
from Voovan |
static StackTraceElement[] |
getStackTrace() |
static StackTraceElement |
getStackTraceElement(int i)
获得堆栈项
|
static Thread[] |
getThreads()
获取JVM中与当前线程同组的所有线程
|
static Thread[] |
getThreads(ThreadGroup group)
获取JVM中与当前线程同组的所有线程
使用数组二次拷贝方式,防止在线程列表获取过程中线程终止 from Voovan |
static void |
interrupt(Thread thread,
boolean isJoin)
结束线程,调用此方法后,线程将抛出
InterruptedException 异常 |
static <T> CompletionService<T> |
newCompletionService()
新建一个CompletionService,调用其submit方法可以异步执行多个任务,最后调用take方法按照完成的顺序获得其结果。
|
static <T> CompletionService<T> |
newCompletionService(ExecutorService executor)
新建一个CompletionService,调用其submit方法可以异步执行多个任务,最后调用take方法按照完成的顺序获得其结果。
|
static CountDownLatch |
newCountDownLatch(int threadCount)
新建一个CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
|
static ThreadPoolExecutor |
newExecutor()
获得一个新的线程池,默认的策略如下:
1.
|
static ThreadPoolExecutor |
newExecutor(int corePoolSize)
新建一个线程池,默认的策略如下:
1.
|
static ThreadPoolExecutor |
newExecutor(int corePoolSize,
int maximumPoolSize)
获得一个新的线程池
如果maximumPoolSize >= corePoolSize,在没有新任务加入的情况下,多出的线程将最多保留60s |
static ThreadPoolExecutor |
newExecutor(int corePoolSize,
int maximumPoolSize,
int maximumQueueSize)
获得一个新的线程池,并指定最大任务队列大小
如果maximumPoolSize >= corePoolSize,在没有新任务加入的情况下,多出的线程将最多保留60s |
static ThreadPoolExecutor |
newExecutorByBlockingCoefficient(float blockingCoefficient)
获得一个新的线程池
传入阻塞系数,线程池的大小计算公式为:CPU可用核心数 / (1 - 阻塞因子) Blocking Coefficient(阻塞系数) = 阻塞时间/(阻塞时间+使用CPU的时间) 计算密集型任务的阻塞系数为0,而IO密集型任务的阻塞系数则接近于1。 |
static ThreadPoolExecutor |
newFixedExecutor(int nThreads,
int maximumQueueSize,
String threadNamePrefix,
boolean isBlocked)
获取一个新的线程池,默认的策略如下
1. |
static ThreadPoolExecutor |
newFixedExecutor(int nThreads,
int maximumQueueSize,
String threadNamePrefix,
RejectedExecutionHandler handler)
获得一个新的线程池,默认策略如下
1. |
static ThreadPoolExecutor |
newFixedExecutor(int nThreads,
String threadNamePrefix,
boolean isBlocked)
获取一个新的线程池,默认的策略如下
1. |
static ThreadFactory |
newNamedThreadFactory(String prefix,
boolean isDaemon)
创建线程工厂
|
static ThreadFactory |
newNamedThreadFactory(String prefix,
ThreadGroup threadGroup,
boolean isDaemon)
创建线程工厂
|
static ThreadFactory |
newNamedThreadFactory(String prefix,
ThreadGroup threadGroup,
boolean isDaemon,
Thread.UncaughtExceptionHandler handler)
创建线程工厂
|
static ExecutorService |
newSingleExecutor()
获得一个新的线程池,只有单个线程,策略如下:
1.
|
static Thread |
newThread(Runnable runnable,
String name)
创建新线程,非守护线程,正常优先级,线程组与当前线程的线程组一致
|
static Thread |
newThread(Runnable runnable,
String name,
boolean isDaemon)
创建新线程
|
static boolean |
safeSleep(long millis)
考虑
Thread.sleep(long) 方法有可能时间不足给定毫秒数,此方法保证sleep时间不小于给定的毫秒数 |
static boolean |
safeSleep(Number millis)
考虑
Thread.sleep(long) 方法有可能时间不足给定毫秒数,此方法保证sleep时间不小于给定的毫秒数 |
static ScheduledThreadPoolExecutor |
schedule(ScheduledThreadPoolExecutor executor,
Runnable command,
long initialDelay,
long period,
boolean fixedRateOrFixedDelay)
开始执行一个定时任务,执行方式分fixedRate模式和fixedDelay模式。
|
static ScheduledThreadPoolExecutor |
schedule(ScheduledThreadPoolExecutor executor,
Runnable command,
long initialDelay,
long period,
TimeUnit timeUnit,
boolean fixedRateOrFixedDelay)
开始执行一个定时任务,执行方式分fixedRate模式和fixedDelay模式。
|
static boolean |
sleep(long millis)
挂起当前线程
|
static boolean |
sleep(Number millis)
挂起当前线程
|
static boolean |
sleep(Number timeout,
TimeUnit timeUnit)
挂起当前线程
|
static void |
sync(Object obj)
阻塞当前线程,保证在main方法中执行不被退出
|
static void |
waitForDie()
等待当前线程结束.
|
static void |
waitForDie(Thread thread)
等待线程结束.
|
public static ThreadPoolExecutor newExecutor(int corePoolSize)
1. 初始线程数为corePoolSize指定的大小 2. 没有最大线程数限制 3. 默认使用LinkedBlockingQueue,默认队列大小为1024
corePoolSize
- 同时执行的线程数大小public static ThreadPoolExecutor newExecutor()
1. 初始线程数为 0 2. 最大线程数为Integer.MAX_VALUE 3. 使用SynchronousQueue 4. 任务直接提交给线程而不保持它们
public static ExecutorService newSingleExecutor()
1. 初始线程数为 1 2. 最大线程数为 1 3. 默认使用LinkedBlockingQueue,默认队列大小为1024 4. 同时只允许一个线程工作,剩余放入队列等待,等待数超过1024报错
public static ThreadPoolExecutor newExecutor(int corePoolSize, int maximumPoolSize)
corePoolSize
- 初始线程池大小maximumPoolSize
- 最大线程池大小ThreadPoolExecutor
public static ThreadPoolExecutor newExecutor(int corePoolSize, int maximumPoolSize, int maximumQueueSize)
corePoolSize
- 初始线程池大小maximumPoolSize
- 最大线程池大小maximumQueueSize
- 最大任务队列大小ThreadPoolExecutor
public static ThreadPoolExecutor newExecutorByBlockingCoefficient(float blockingCoefficient)
see: http://blog.csdn.net/partner4java/article/details/9417663
blockingCoefficient
- 阻塞系数,阻塞因子介于0~1之间的数,阻塞因子越大,线程池中的线程数越多。ThreadPoolExecutor
public static ThreadPoolExecutor newFixedExecutor(int nThreads, String threadNamePrefix, boolean isBlocked)
1. 核心线程数与最大线程数为nThreads指定的大小 2. 默认使用LinkedBlockingQueue,默认队列大小为1024 3. 如果isBlocked为true
,当执行拒绝策略的时候会处于阻塞状态,直到能添加到队列中或者被Thread.interrupt()
中断
nThreads
- 线程池大小threadNamePrefix
- 线程名称前缀isBlocked
- 是否使用BlockPolicy
策略public static ThreadPoolExecutor newFixedExecutor(int nThreads, int maximumQueueSize, String threadNamePrefix, boolean isBlocked)
1. 核心线程数与最大线程数为nThreads指定的大小 2. 默认使用LinkedBlockingQueue 3. 如果isBlocked为true
,当执行拒绝策略的时候会处于阻塞状态,直到能添加到队列中或者被Thread.interrupt()
中断
nThreads
- 线程池大小maximumQueueSize
- 队列大小threadNamePrefix
- 线程名称前缀isBlocked
- 是否使用BlockPolicy
策略public static ThreadPoolExecutor newFixedExecutor(int nThreads, int maximumQueueSize, String threadNamePrefix, RejectedExecutionHandler handler)
1. 核心线程数与最大线程数为nThreads指定的大小 2. 默认使用LinkedBlockingQueue
nThreads
- 线程池大小maximumQueueSize
- 队列大小threadNamePrefix
- 线程名称前缀handler
- 拒绝策略public static void execute(Runnable runnable)
runnable
- 可运行对象public static Runnable execAsync(Runnable runnable, boolean isDaemon)
runnable
- 需要执行的方法体isDaemon
- 是否守护线程。守护线程会在主线程结束后自动结束public static <T> Future<T> execAsync(Callable<T> task)
T
- 回调对象类型task
- Callable
public static Future<?> execAsync(Runnable runnable)
runnable
- 可运行对象Future
public static <T> CompletionService<T> newCompletionService()
T
- 回调对象类型public static <T> CompletionService<T> newCompletionService(ExecutorService executor)
T
- 回调对象类型executor
- 执行器 ExecutorService
public static CountDownLatch newCountDownLatch(int threadCount)
threadCount
- 线程数量public static Thread newThread(Runnable runnable, String name)
public static boolean sleep(Number timeout, TimeUnit timeUnit)
timeout
- 挂起的时长timeUnit
- 时长单位public static boolean sleep(Number millis)
millis
- 挂起的毫秒数public static boolean sleep(long millis)
millis
- 挂起的毫秒数public static boolean safeSleep(Number millis)
Thread.sleep(long)
方法有可能时间不足给定毫秒数,此方法保证sleep时间不小于给定的毫秒数millis
- 给定的sleep时间sleep(Number)
public static boolean safeSleep(long millis)
Thread.sleep(long)
方法有可能时间不足给定毫秒数,此方法保证sleep时间不小于给定的毫秒数millis
- 给定的sleep时间sleep(Number)
public static StackTraceElement[] getStackTrace()
public static StackTraceElement getStackTraceElement(int i)
i
- 第几个堆栈项public static <T> ThreadLocal<T> createThreadLocal(boolean isInheritable)
T
- 持有对象类型isInheritable
- 是否为子线程提供从父线程那里继承的值public static <T> ThreadLocal<T> createThreadLocal(Supplier<? extends T> supplier)
T
- 持有对象类型supplier
- 初始化线程对象函数ThreadLocal.withInitial(Supplier)
public static ThreadFactoryBuilder createThreadFactoryBuilder()
ThreadFactoryBuilder.build()
public static ThreadFactory createThreadFactory(String threadNamePrefix)
ThreadFactory
threadNamePrefix
- 线程名称前缀ThreadFactory
ThreadFactoryBuilder.build()
public static void interrupt(Thread thread, boolean isJoin)
InterruptedException
异常thread
- 线程isJoin
- 是否等待结束public static void waitForDie()
Thread.join()
并忽略 InterruptedException
public static void waitForDie(Thread thread)
Thread.join()
并忽略 InterruptedException
thread
- 线程public static Thread[] getThreads()
public static Thread[] getThreads(ThreadGroup group)
group
- 线程组public static Thread getMainThread()
public static ThreadGroup currentThreadGroup()
public static ThreadFactory newNamedThreadFactory(String prefix, boolean isDaemon)
prefix
- 线程名前缀isDaemon
- 是否守护线程ThreadFactory
public static ThreadFactory newNamedThreadFactory(String prefix, ThreadGroup threadGroup, boolean isDaemon)
prefix
- 线程名前缀threadGroup
- 线程组,可以为nullisDaemon
- 是否守护线程ThreadFactory
public static ThreadFactory newNamedThreadFactory(String prefix, ThreadGroup threadGroup, boolean isDaemon, Thread.UncaughtExceptionHandler handler)
prefix
- 线程名前缀threadGroup
- 线程组,可以为nullisDaemon
- 是否守护线程handler
- 未捕获异常处理ThreadFactory
public static void sync(Object obj)
obj
- 对象所在线程public static ConcurrencyTester concurrencyTest(int threadSize, Runnable runnable)
threadSize
- 并发线程数runnable
- 执行的逻辑实现ConcurrencyTester
public static ScheduledThreadPoolExecutor createScheduledExecutor(int corePoolSize)
corePoolSize
- 初始线程池大小ScheduledThreadPoolExecutor
public static ScheduledThreadPoolExecutor schedule(ScheduledThreadPoolExecutor executor, Runnable command, long initialDelay, long period, boolean fixedRateOrFixedDelay)
executor
- 定时任务线程池,null
新建一个默认线程池command
- 需要定时执行的逻辑initialDelay
- 初始延迟,单位毫秒period
- 执行周期,单位毫秒fixedRateOrFixedDelay
- true
表示fixedRate模式,false
表示fixedDelay模式ScheduledThreadPoolExecutor
public static ScheduledThreadPoolExecutor schedule(ScheduledThreadPoolExecutor executor, Runnable command, long initialDelay, long period, TimeUnit timeUnit, boolean fixedRateOrFixedDelay)
executor
- 定时任务线程池,null
新建一个默认线程池command
- 需要定时执行的逻辑initialDelay
- 初始延迟period
- 执行周期timeUnit
- 时间单位fixedRateOrFixedDelay
- true
表示fixedRate模式,false
表示fixedDelay模式ScheduledThreadPoolExecutor
Copyright © 2024. All rights reserved.