一、将设置超时的代码放入线程中,我这边使用了线程池工具类, 也可直接ExecutorService thread= Executors.newCachedThreadPool(),建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
//使用线程池获取线程
ExecutorService thread = ThreadPoolUtils.getThread();
// 使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中
FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
try {
// 真正的任务代码在这里执行,返回值为你需要的类型
} catch (Exception e) {
LOG.error("", e);
}
return "";
}
});
try {
//执行任务
thread.execute(future);
//任务处理超时时间设为 30秒
String obj = future.get(1000 * 30, TimeUnit.MILLISECONDS);
LOG.info("任务成功返回:" + obj);
return obj;
} catch (TimeoutException ex) {
LOG.error("任务处理超时....", ex);
ex.printStackTrace();
return "";
} catch (Exception e) {
LOG.error("处理失败.");
e.printStackTrace();
return "";
}
}
TimeUnit.MILLISECONDS (3000) 线程等待3秒,TimeUnit.SECONDS(30)线程等待30秒,这两个都可设置超时时间,只是时间单位不同
线程池使用主要为了解决一下几个问题:
通过重用线程池中的线程,来减少每个线程创建和销毁的性能开销
对线程进行一些维护和管理,比如定时开始,周期执行,并发数控制等
以下线程池工具类:
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import java.util.concurrent.*;
/**
* @date: 2018/7/12
* @description:
*/
public class ThreadPoolUtils {
private static int corePoolSize = 200;
private static int taskPoolSize = 20;
private static int maxPoolSize = 2000;
private static long keepAliveTime = 1;
private static TimeUnit timeUnit = TimeUnit.MINUTES;
private static BlockingQueue<Runnable> queue = new SynchronousQueue<>();
private static ExecutorService executorService;
private static ScheduledExecutorService scheduledExecutorService;
private static ThreadPoolTaskScheduler threadPoolTaskScheduler;
private ThreadPoolUtils() {
throw new IllegalStateException("Utility class");
}
public static ExecutorService getThread() {
if (executorService == null) {
executorService = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, queue);
}
return executorService;
}
public static ScheduledExecutorService getSchedule() {
if (scheduledExecutorService == null) {
scheduledExecutorService = new ScheduledThreadPoolExecutor(corePoolSize);
}
return scheduledExecutorService;
}
public static ThreadPoolTaskScheduler getTaskSchedule() {
if (threadPoolTaskScheduler == null) {
threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(taskPoolSize);
threadPoolTaskScheduler.initialize();
}
return threadPoolTaskScheduler;
}
}
简单的多线程
package com.liang.test;
import com.liang.utils.ThreadPoolUtils;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
public class Test2 {
public static void main(String[] args) {
final int w=10;
for (int i = 0; i < 10; i++) {
//使用线程池获取线程
ExecutorService thread = ThreadPoolUtils.getThread();
// System.out.println(thread);
// 使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中
int finalI = i;
FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
// 真正的任务代码在这里执行,返回值为你需要的类型
Thread.sleep(1000*(10-finalI));
System.out.println(finalI);
//
return "";
}
});
//执行任务
System.out.println("第"+i+"次前");
//下面这行代码不会起到阻塞作用
thread.execute(future);
System.out.println("第"+i+"次后");
}
}
}