是一个包含循环线程安全的静态方法吗?

时间:2014-08-09 11:19:40

标签: java multithreading loops

我有一个巨大的循环,我想分成4个线程。我使用了一点点noobish方法(或者可能没有?)并将for循环的计数器分成4个区间,为每个线程创建了一个新的Printwriter和CrucibleOptimizer,这样就没有冲突了,比如说这样:

    public static void main(String[] args) {
    Runnable run1 = new Runnable() {
        @Override
        public void run() {
            PrintWriter writer1;
            try {
                writer1 = new PrintWriter("test_result1.txt");
                CrucibleOptimizer optimizer1 = new CrucibleOptimizer();
                int[] loop1boundries = new int[]{1, 7};
                opt(optimizer1, writer1, loop1boundries[0], loop1boundries[1]);
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
                e.printStackTrace();
            }
        }
    };
    Runnable run2 = new Runnable() {
        @Override
        public void run() {
            PrintWriter writer2;
            try {
                writer2 = new PrintWriter("test_result2.txt");
                CrucibleOptimizer optimizer2 = new CrucibleOptimizer();
                int[] loop2boundries = new int[]{8, 14};
                opt(optimizer2, writer2, loop2boundries[0], loop2boundries[1]);
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
                e.printStackTrace();
            }
        }
    };
    Runnable run3 = new Runnable() {
        @Override
        public void run() {
            PrintWriter writer3;
            try {
                writer3 = new PrintWriter("test_result3.txt");
                CrucibleOptimizer optimizer3 = new CrucibleOptimizer();
                int[] loop3boundries = new int[]{15, 22};
                opt(optimizer3, writer3, loop3boundries[0], loop3boundries[1]);
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
                e.printStackTrace();
            }
        }
    };
    Runnable run4 = new Runnable() {
        @Override
        public void run() {
            PrintWriter writer4;
            try {
                writer4 = new PrintWriter("test_result4.txt");
                CrucibleOptimizer optimizer4 = new CrucibleOptimizer();
                int[] loop4boundries = new int[]{23, 30};
                opt(optimizer4, writer4, loop4boundries[0], loop4boundries[1]);
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
                e.printStackTrace();
            }
        }
    };
    Thread[] threads = new Thread[]{new Thread(run1), new Thread(run2), new Thread(run3), new Thread(run4)};
    for (Thread thr : threads){
        thr.start();
    }
}

这就是我要问的方法。我不知道它的线程是否安全。我一直在读书,google说我没有任何局部变量,我很好,但关注的是这些循环中的多个计数器:

    public static void opt(CrucibleOptimizer opt, PrintWriter writer, int minIncluded, int maxIncluded){
    //more than this is never used
    final int oreMaterialsMaximum = 100;//100
    final int ingotMaterialMaximum = 30;//30

    //test for every possible material combination
    for (int a = minIncluded; a <= maxIncluded; a++){//for amount of ingots
        System.out.println("Testing for ingot number: " + a);
        double ratioMin = (Reference.UNITS_IMPOSSIBLE / (double)(a * Reference.UNITS_INGOT));
        for (int i = 0; i <= (int)(100 / Reference.UNITS_IMPOSSIBLE); i++){//for every ratio possible
            double currentRatio = round(i * ratioMin, 6);
            System.out.println("Testing for ratio: " + currentRatio);
            for (int b = 0; b <= ingotMaterialMaximum; b++){//with every amount of ingots
                for (int c = 0; c <= oreMaterialsMaximum; c++){//with every amount of rich ore
                    for (int d = 0; d <= oreMaterialsMaximum; d++){//with every amount of normal ore
                        for (int e = 0; e <= oreMaterialsMaximum; e++){//with every amount of poor ore
                            for (int f = 0; f <= oreMaterialsMaximum; f++){//with every amount of small ore
                                opt.set(null, null, null, a); //only the ingots are passed in this way
                                int[] res = opt.optimizeMaterial(new int[]{c, d, e, f, b}, currentRatio);
                                if (res != null){
                                    int units = 0;
                                    for (int j = 0; j < res.length; j++)
                                        units += res[j] * Reference.MATERIAL_UNITS[j];
                                    double unitsRight = Math.round(a * Reference.UNITS_INGOT * currentRatio);
                                    if (units != (int)unitsRight){ //if the units are not correct, log
                                        writer.println("I: " + a + " Rat: " + currentRatio + " I_av: " + b + " O_Ri: " + c + " O_No: " + d +
                                                " O_Po: " + e + " O_Sm: " + f + " units_wrong: " + units + " units_right: " + (int)unitsRight);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    System.out.println("Testing done");
    writer.close();
}

2 个答案:

答案 0 :(得分:1)

“不使用静态变量”建议确实过于简单:另一个要求是不将共享对象传递给在不同线程中运行的static方法。

循环计数器和其他原始局部变量是线程安全的。唯一可以使方法非线程安全的是共享状态。您似乎已成功通过创建单独的CrucibleOptimizerPrintWriter对象来避免这种情况。

我要尝试的一个重构是合并你的Runnable。创建一个带有循环边界的命名类,并在main中创建该类的四个实例。这比四个差异很小的匿名类更有效:

private static class ThreadRunnable implements Runnable {
    final String fileName;
    final int[] loopBoundaries;
    public ThreadRunnable(String fn, int[] lb) {
        fileName = fn;
        loopBoundaries = lb;
    }
    @Override
    public void run() {
        PrintWriter pw;
        try {
            pw = new PrintWriter(fileName);
            CrucibleOptimizer co = new CrucibleOptimizer();
            opt(co, pw, loop4boundries[0], loop4boundries[1]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

现在,您可以创建四个共享相同代码的ThreadRunnable个实例。

答案 1 :(得分:0)

循环本身是线程安全的,所以不需要担心。

您唯一需要担心的是一次可以由多个线程访问的任何内容。

然而,您的整个架构确实需要一些工作。

例如,为什么有4个单独的可运行实现,而不是有一个实现并将参数传递给它来说明要处理哪个块。

我也不知道你要对所有循环做什么,但你不太可能真的需要这样的结构。