多线程FutureTask未并行运行

坑:

FutureTask的get()方法会对当前线程堵塞直到拿到结果。

如果循环体中创建多个线程,要在循环体外面get()获取结果,否则虽然是多线程,但是会一直处于堵塞状态

实际只有一个子线程在跑。

详情查看此篇文章:

https://blog.csdn.net/hykwhjc/article/details/87884328

 

代码示例:

public class Test {
    public static void main(String[] args) {
 
        long startMili=System.currentTimeMillis();
        //启用5个线程
        ExecutorService executor = Executors.newFixedThreadPool(5);
        System.out.println("主线程在执行任务");
 
        try {
 
            int n=0;
 
            for (int i = 0; i < 10; i++) {
 
                FutureTask futureTask = new FutureTask(new Task(i));
                executor.submit(futureTask);
                n+=futureTask.get();
 
            }
            executor.shutdown();
            System.out.println("task运行结果"+n);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        long endMili=System.currentTimeMillis();
 
        System.out.println("所有任务执行完毕,用时"+(endMili-startMili));
    }
}

运行结果

主线程在执行任务
pool-1-thread-1子线程在进行计算,计算结果0
pool-1-thread-2子线程在进行计算,计算结果0
pool-1-thread-3子线程在进行计算,计算结果1
pool-1-thread-4子线程在进行计算,计算结果3
pool-1-thread-5子线程在进行计算,计算结果6
pool-1-thread-1子线程在进行计算,计算结果10
pool-1-thread-2子线程在进行计算,计算结果15
pool-1-thread-3子线程在进行计算,计算结果21
pool-1-thread-4子线程在进行计算,计算结果28
pool-1-thread-5子线程在进行计算,计算结果36
task运行结果120
所有任务执行完毕,用时10051
 


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
 
/**
 * @desc:
 * @Author: Yongkang Hou
 * @Date: 2019/2/22
 */
public class Test {
    public static void main(String[] args) {
   
        long startMili = System.currentTimeMillis ();
        //启用5个线程
        ExecutorService executor = Executors.newFixedThreadPool (5);
        System.out.println ("主线程在执行任务");
        try {
 
            int n = 0;
            List > futureTaskList = new ArrayList <> ();
            for (int i = 0; i < 10; i++) {
 
                FutureTask  futureTask = new FutureTask <> (new Task (i));
                executor.submit (futureTask);
                futureTaskList.add (futureTask);
 
            }
            for (FutureTask  integerFutureTask : futureTaskList) {
                n += integerFutureTask.get ();
            }
            executor.shutdown ();
            System.out.println ("task运行结果" + n);
        } catch (Exception e) {
            e.printStackTrace ();
        }
        long endMili = System.currentTimeMillis ();
        System.out.println ("所有任务执行完毕,用时" + (endMili - startMili));
    }
}

运行结果

主线程在执行任务
pool-1-thread-1子线程在进行计算,计算结果0
pool-1-thread-3子线程在进行计算,计算结果1
pool-1-thread-4子线程在进行计算,计算结果3
pool-1-thread-2子线程在进行计算,计算结果0
pool-1-thread-5子线程在进行计算,计算结果6
pool-1-thread-3子线程在进行计算,计算结果10
pool-1-thread-2子线程在进行计算,计算结果28
pool-1-thread-4子线程在进行计算,计算结果21
pool-1-thread-1子线程在进行计算,计算结果15
pool-1-thread-5子线程在进行计算,计算结果36
task运行结果120
所有任务执行完毕,用时2023
 

你可能感兴趣的:(哪些年走过的坑)