1.bugdemo
package com.zhenzhen;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
private static ExecutorService executorPool;
static {
executorPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1200));
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();
for(int i=0;i<3;i++) {
FutureTask<Integer> task = new FutureTask<Integer>(new CleanRedisZsetTask());
executorPool.execute(task);
System.out.println(task.get());
}
System.out.println("耗时"+(System.currentTimeMillis()-startTime)/1000);
}
}
class CleanRedisZsetTask implements Callable<Integer> {
@Override
public Integer call() throws Exception {
Thread.sleep(3000);
return 1;
}
}
2.正确使用
package com.brcloud;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
private static ExecutorService executorPool;
static {
executorPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1200));
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();
List<Future<Integer>> taskList = new ArrayList<>();
for(int i=0;i<3;i++) {
taskList.add(executorPool.submit(new CleanRedisZsetTask()));
//System.out.println(task.get());
}
for(Future<Integer> task:taskList) {
task.get();
}
System.out.println("耗时"+(System.currentTimeMillis()-startTime)/1000);
}
}
class CleanRedisZsetTask implements Callable<Integer> {
@Override
public Integer call() throws Exception {
Thread.sleep(3000);
return 1;
}
}
3.如何把执行结果保存到线程的变量,循环取出
package com.brcloud;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
private static ExecutorService executorPool;
static {
executorPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1200));
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
long startTime = System.currentTimeMillis();
List<Future<Integer>> taskList = new ArrayList<>();
List<CleanRedisZsetTask> tasks = new ArrayList<>();
for(int i=0;i<3;i++) {
CleanRedisZsetTask cleanRedisZsetTask = new CleanRedisZsetTask("张三"+i,23);
tasks.add(cleanRedisZsetTask);
taskList.add(executorPool.submit(cleanRedisZsetTask));
//System.out.println(task.get());
}
int i = 0;
for(CleanRedisZsetTask cleanRedisZsetTask:tasks) {
System.out.println(cleanRedisZsetTask);
taskList.get(i).get();
i++;
}
System.out.println("耗时"+(System.currentTimeMillis()-startTime)/1000);
}
}
class CleanRedisZsetTask implements Callable<Integer> {
private String name;
private int age;
@Override
public String toString() {
return "CleanRedisZsetTask [name=" + name + ", age=" + age + "]";
}
public CleanRedisZsetTask(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public Integer call() throws Exception {
Thread.sleep(3000);
return 1;
}
}