public class ThreadUtils extends Object
| Constructor and Description |
|---|
ThreadUtils() |
| Modifier and Type | Method and Description |
|---|---|
static ThreadBuilder |
createThreadFactoryBuilder()
创建ThreadFactoryBuilder
|
static <T> ThreadLocal<T> |
createThreadLocal(boolean isInheritable)
创建本地线程对象
|
static ThreadGroup |
currentThreadGroup()
获取当前线程的线程组
|
static Runnable |
excAsync(Runnable runnable,
boolean isDeamon)
执行异步方法
|
static <T> Future<T> |
execAsync(Callable<T> task)
执行有返回值的异步方法
Future代表一个异步执行的操作,通过get()方法可以获得操作的结果,如果异步操作还没有完成,则,get()会使当前线程阻塞
|
static Future<?> |
execAsync(Runnable runnable)
执行有返回值的异步方法
Future代表一个异步执行的操作,通过get()方法可以获得操作的结果,如果异步操作还没有完成,则,get()会使当前线程阻塞
|
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 |
interupt(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 ExecutorService |
newExecutor()
获得一个新的线程池
|
static ExecutorService |
newExecutor(int threadSize)
新建一个线程池
|
static ThreadPoolExecutor |
newExecutor(int corePoolSize,
int maximumPoolSize)
获得一个新的线程池
如果maximumPoolSize =》 corePoolSize,在没有新任务加入的情况下,多出的线程将最多保留60s
|
static ThreadPoolExecutor |
newExecutorByBlockingCoefficient(float blockingCoefficient)
获得一个新的线程池
传入阻塞系数,线程池的大小计算公式为:CPU可用核心数 / (1 - 阻塞因子)
Blocking Coefficient(阻塞系数) = 阻塞时间/(阻塞时间+使用CPU的时间)
计算密集型任务的阻塞系数为0,而IO密集型任务的阻塞系数则接近于1
|
static ThreadFactory |
newNamedThreadFactory(String prefix,
boolean isDeamon)
创建线程工厂
|
static ThreadFactory |
newNamedThreadFactory(String prefix,
ThreadGroup threadGroup,
boolean isDeamon)
创建线程工厂
|
static ThreadFactory |
newNamedThreadFactory(String prefix,
ThreadGroup threadGroup,
boolean isDeamon,
Thread.UncaughtExceptionHandler handler)
创建线程工厂
|
static ExecutorService |
newSingleExecutor()
获得一个新的线程池,只有单个线程
|
static Thread |
newThread(Runnable runnable,
String name)
创建新线程,非守护线程,正常优先级,线程组与当前线程的线程组一致
|
static Thread |
newThread(Runnable runnable,
String name,
boolean isDeamon)
创建新线程
|
static boolean |
safeSleep(Number millis)
考虑
Thread.sleep(long)方法有可能时间不足给定毫秒数,此方法保证sleep时间不小于给定的毫秒数 |
static boolean |
sleep(Number millis)
挂起当前线程
|
static boolean |
sleep(Number timeout,
TimeUnit timeUnit)
挂起当前线程
|
static void |
sync(Object obj)
阻塞当前线程,保证在main方法中执行不被退出
|
static void |
waitForDie(Thread thread)
等待线程结束.
|
public static ExecutorService newExecutor(int threadSize)
threadSize - 同时执行的线程数大小public static ExecutorService newExecutor()
public static ExecutorService newSingleExecutor()
public static ThreadPoolExecutor newExecutor(int corePoolSize, int maximumPoolSize)
corePoolSize - 初始线程池大小maximumPoolSize - 最大线程池大小ThreadPoolExecutorpublic static ThreadPoolExecutor newExecutorByBlockingCoefficient(float blockingCoefficient)
see: http://blog.csdn.net/partner4java/article/details/9417663
blockingCoefficient - 阻塞系数,阻塞因子介于0~1之间的数,阻塞因子越大,线程池中的线程数越多ThreadPoolExecutorpublic static void execute(Runnable runnable)
runnable - 可运行对象public static Runnable excAsync(Runnable runnable, boolean isDeamon)
runnable - 需要执行的方法体isDeamon - 是否守护线程 守护线程会在主线程结束后自动结束public static <T> Future<T> execAsync(Callable<T> task)
T - 回调对象类型task - Callablepublic static Future<?> execAsync(Runnable runnable)
runnable - 可运行对象Futurepublic static <T> CompletionService<T> newCompletionService()
T - 回调对象类型public static <T> CompletionService<T> newCompletionService(ExecutorService executor)
T - 回调对象类型executor - 执行器 ExecutorServicepublic 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 safeSleep(Number 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 ThreadBuilder createThreadFactoryBuilder()
ThreadBuilder.build()public static void interupt(Thread thread, boolean isJoin)
InterruptedException异常thread - 线程isJoin - 是否等待结束public static void waitForDie(Thread thread)
Thread.join() 并忽略 InterruptedExceptionthread - 线程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 isDeamon)
prefix - 线程名前缀isDeamon - 是否守护线程public static ThreadFactory newNamedThreadFactory(String prefix, ThreadGroup threadGroup, boolean isDeamon)
prefix - 线程名前缀threadGroup - 线程组,可以为nullisDeamon - 是否守护线程public static ThreadFactory newNamedThreadFactory(String prefix, ThreadGroup threadGroup, boolean isDeamon, Thread.UncaughtExceptionHandler handler)
prefix - 线程名前缀threadGroup - 线程组,可以为nullisDeamon - 是否守护线程handler - 未捕获异常处理public static void sync(Object obj)
obj - 对象所在线程Copyright © 2019. All rights reserved.