Java线程完成运行后将运行代码

时间:2019-05-06 15:11:23

标签: java multithreading wait

在线程完成运行后,我需要运行一些代码。我从每个线程中获得了一些准确性,并将它们存储在列表中。

我想找到一种方法,让我在线程执行后最后执行代码以获取准确性。

        for (int i = 0; i < 30; i++) {

            final int iVal = i;
            dataSet.randomize(new Random(iVal));

            int trainSize = (int) Math.round(dataSet.numInstances() * 0.7);
            int testSize = dataSet.numInstances() - trainSize;

            Instances train = new Instances(dataSet, 0, trainSize);
            Instances test = new Instances(dataSet, trainSize, testSize);

            new Thread(new Runnable() {

                public void run() {
                    try {

                        accuraciesList.add(runThread(classifierName, fileName, path, dataFile, iVal, train, test, testSize));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }

        double sum = 0;
        for (double d : accuraciesList) {
            sum = sum + d;
        }
        double avgAccuracy = sum / 30;

        System.out.println("\n \n AVG ACCURACY : " + avgAccuracy);

当前列表为空,因为代码在线程之前运行。

0 个答案:

没有答案