线程使用比较广泛,但实际上一般项目很少用上线程,线程常用于优化复杂的程序执行流程,把一些与业务关系关系不大但是必须要执行的流程使用线程的方式让子线程去执行,主流程只返回跟业务有关的信息
runnable是无返回值的执行线程;callable是有返回值的执行线程
实现runable接口的实现类通常使用execute来开启线程池中的线程来执行任务,但是也支持submit来开启原则上不会报错,因为实现类返回结果就是void
实现callable接口的实现类只能用submit来开启线程池中的线程来执行任务
下面是测试代码:
public class TestThread { private static int nThreads = Runtime.getRuntime().availableProcessors() * 2 + 1; private static ExecutorService executors = Executors.newFixedThreadPool(nThreads, new ThreadFactory() { private final String threadNamePrefix = "si_query_task_"; private final AtomicInteger count = new AtomicInteger(1); @Override
public Thread newThread(Runnable r) {
Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, threadNamePrefix + count.getAndIncrement());
t.setDaemon(true);
return t;
}
}); public static void main(String[] args) { List<Future<String[]>> fList = new ArrayList<>(); for (int i = 0; i < 10; i++) {
final int nextInt = new Random().nextInt(100);
Future<String[]> f = executors.submit(new TestTaskcb(nextInt));
fList.add(f);
} for (Future<String[]> future : fList) {
try {
String[] result = future.get();
// System.out.println(result[0] + " , 结果 " + result[1]);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
System.out.println("call线程结束"); for (int i = 0; i < 10; i++) {
final int nextInt = new Random().nextInt(100);
executors.execute(new TestTaskvoid(nextInt));
}
System.out.println("void线程结束"); System.out.println("main 线程结束 "); } static class TestTaskcb implements Callable<String[]> { private int i; public TestTaskcb(int i) {
this.i = i;
} @Override
public String[] call() throws Exception {
String result = i % 2 == 0 ? "S" : "F";
// 业务处理逻辑
//Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "第" + i + "次任务");
return new String[]{ Thread.currentThread().getName(), result };
}
} static class TestTaskvoid implements Runnable { private int i; public TestTaskvoid(int i) {
this.i = i;
} @Override
public void run() {
System.out.println(Thread.currentThread().getName() + "第" + i + "次任务");
}
}
}
线程执行是随机执行的,打印结果不可作参考