线程数量的增加会导致更快的执行时间?

时间:2017-02-22 10:50:48

标签: java multithreading

在下面的代码中,我在前面的帖子中读到,与较少的线程相比,有些人遇到更多线程的运行时间更快。但是,基于我对线程的理解,我理解由于上下文切换,它会导致更多线程的总体时间增加。但是,当我运行这段代码时,我的执行时间严格减少。我是错误地实现了这个,还是仅仅因为我的系统。 (我正在使用MacBook Pro 2015)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class MeanThread {   
    public static void main(String[] args) throws InterruptedException, FileNotFoundException {
        Scanner s = new Scanner(new File("/Users/Bryan/Desktop/personal/Code/Java/Lab 2/src/input.txt"));
        ArrayList<Integer> listOfValues = new ArrayList<Integer>();
        ArrayList<Double> temporalMeanArray = new ArrayList<Double>();
        Double temporalMean = 0.0;
        while (s.hasNextInt()) {
            int number = s.nextInt();
            listOfValues.add(number);
        }
        // define number of threads
        // this way, you can pass number of threads as 
        // a second command line argument at runtime.
        int NumOfThread = Integer.parseInt(args[0]);
        // TODO: partition the array list into N subArrays, where N is the number of threads
        ArrayList<ArrayList<Integer>> listOfSubArrays = new ArrayList<ArrayList<Integer>>(NumOfThread);
        for (int i = 0; i < listOfValues.size(); i += NumOfThread) {
            listOfSubArrays.add(new ArrayList<Integer>(listOfValues.subList(i, Math.min(i + NumOfThread, listOfValues.size()))));
        }
        // TODO: start recording time
        //Tip: you can't create big number of threads in the above way. So, create an array list of threads. 
        ArrayList<MeanMultiThread> threadList = new ArrayList<MeanMultiThread>();
        for (int i = 0; i < listOfSubArrays.size(); i ++) {
            threadList.add(new MeanMultiThread(listOfSubArrays.get(i)));
        }
        // TODO: start each thread to execute your computeMean() function defined under the run() method
           //so that the N mean values can be computed. for example,
        int threadCounter = 1;
        long startTime = System.currentTimeMillis();
        for (MeanMultiThread mmt : threadList) {
            mmt.start();
        }

        for (MeanMultiThread mmt : threadList) {
            mmt.join();
            System.out.println("Temporal mean value of thread " + threadCounter + " is " + mmt.getMean());
            threadCounter ++;
            temporalMeanArray.add(mmt.getMean());
        }

        for (Double d : temporalMeanArray) {
            temporalMean += d;
        }
        Double globalMean = temporalMean/temporalMeanArray.size();
        long endTime = System.currentTimeMillis();
        long elapsedTime = endTime - startTime;
        System.out.println("Total elapsed time is " + elapsedTime + " milliseconds.");
        System.out.println("The global mean value is " + globalMean);

    }
}
//Extend the Thread class
class MeanMultiThread extends Thread {
    private ArrayList<Integer> list;
    private double mean;
    int counter = 0;
    MeanMultiThread(ArrayList<Integer> array) {
        list = array;
    }
    public double getMean() {
        return mean;
    }
    public void run() {
        mean = computeMean(list);

    }
    private double computeMean(ArrayList<Integer> list2) {
        double totalAmount = 0.0;
        for (int i : list2) {
            totalAmount += i;
        }
        return totalAmount/(list2.size());
    }
}

1 个答案:

答案 0 :(得分:2)

如果增加线程数(args[0]),则实际使用的线程数会减少。原因是您使用NumOfThread作为子数组的存储区大小。随着您增加铲斗尺寸,您可以减少铲斗,从而减少螺纹。违规行是

for (int i = 0; i < listOfValues.size(); i += NumOfThread) {

一起
for (int i = 0; i < listOfSubArrays.size(); i ++) {

无论线程数量如何,保持桶大小不变,然后您将获得有意义的结果。由于您的工作负载完全受CPU限制,因此只要您拥有的线程多于CPU可以同时处理的线程,就不会获得有意义的加速。