java线程池
Posted z_java_20150812
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java线程池相关的知识,希望对你有一定的参考价值。
package com.example.demo.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class ThreadPoolTest
public static void main(String[] args)
ThreadPoolTest threadPoolTest = new ThreadPoolTest();
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<Future<String>> list = new ArrayList<>();
for (int i = 0; i < 1000; i++)
int temp = i;
Future<String> future = executorService.submit(new Callable<String>()
@Override
public String call() throws InterruptedException
return threadPoolTest.deal(temp);
);
// 如果存在失败,就停止后面的执行
if (!threadPoolTest.getResult(future))
break;
list.add(future);
System.out.println("循环结束了");
for (Future<String> future : list)
try
System.out.println(future.get());
catch (InterruptedException e)
e.printStackTrace();
catch (ExecutionException e)
e.printStackTrace();
System.out.println("方法结束了");
private String deal(int temp)
try
// todo 业务处理
if (temp == 300)
return "fail";
catch (Exception e)
e.printStackTrace();
return "fail";
return "success";
private Boolean getResult(Future<String> future)
try
return future.get().equals("success") ? true : false;
catch (InterruptedException e)
e.printStackTrace();
catch (ExecutionException e)
e.printStackTrace();
return false;
以上是关于java线程池的主要内容,如果未能解决你的问题,请参考以下文章
如何移除网站Response Headers中的X-Powered-By信息?
DouPHP去除Powered by DouPHP版权的方法