Java线程 – 我在编写线程安全的代码吗?

我目前正在努力进行并行处理,所以要做到这一点,我正在编写一个处理图像的程序,总体上提供有关其颜色值的信息 – 我正在对这一个类进行一些测试,其中随机生成的数组整数和4个线程正在运行以处理每个第4个像素,从它们各自的起始位置开始.我只是想知道这个读取是否是线程安全的?多个线程可以读取相同的数据结构,如果这是我想要的吗?

import java.awt.image.BufferedImage;
import java.lang.Thread;


public class ImageProcessor extends Thread {

    public static void main(String[] args) {

        int[] z = new int[10000000];
        for (int i = 0; i < 10000000; i++) {

            double a = (Math.random()*1000000);
            z[i] = (int) a;

        }

        ImageProcessor ip = new ImageProcessor();
        ip.imgRGBPercent(z);
    }


    public ImageProcessor() {

    }

    public void process(int[] x, int startPoint) {

        (new Thread(new ReadThread(x, startPoint))).start();    
    }

    public int[] imgRGBPercent(int[] x) {




        ReadThread first = new ReadThread(x, 0);
        ReadThread second = new ReadThread(x, 1);
        ReadThread third = new ReadThread(x, 2);
        ReadThread fourth = new ReadThread(x, 3);

        Thread a = (new Thread(first));
        Thread b = (new Thread(second));
        Thread c = (new Thread(third));
        Thread d = (new Thread(fourth));

        long timeMetric = System.currentTimeMillis();
        a.start();
        b.start();
        c.start();
        d.start();


        try {

            a.join();
        }
        catch (Exception e) {

        }

        try {

            b.join();
        }
        catch (Exception e) {

        }

        try {

            c.join();
        }
        catch (Exception e) {

        }

        try {

            d.join();
        }
        catch (Exception e) {

        }

        int redTotal, blueTotal, greenTotal;

        redTotal = first.getRGBTotals()[0] + second.getRGBTotals()[0] + third.getRGBTotals()[0] + fourth.getRGBTotals()[0];
        blueTotal = first.getRGBTotals()[1] + second.getRGBTotals()[1] + third.getRGBTotals()[1] + fourth.getRGBTotals()[1];
        greenTotal = first.getRGBTotals()[2] + second.getRGBTotals()[2] + third.getRGBTotals()[2] + fourth.getRGBTotals()[2];

        System.out.println(greenTotal);

        System.out.println(System.currentTimeMillis() - timeMetric);

        timeMetric = System.currentTimeMillis();

        ColorValue cv1 = new ColorValue();
        int sum = 0;
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 0; i < x.length; i++) {

            sum += cv1.getGreen(x[i]);
            sum1 += cv1.getRed(x[i]);
            sum2 += cv1.getBlue(x[i]);
        }


        System.out.println(sum);

        System.out.println(System.currentTimeMillis() - timeMetric);

        int[] out = new int[3];
        return out;
    }


    private class ReadThread implements Runnable {

        private int[] colorArr;
        private int startPoint, redTotal, blueTotal, greenTotal;
        private ColorValue cv;

        public ReadThread(int[] x, int startPoint) {

            colorArr = x;
            this.startPoint = startPoint;
            cv = new ColorValue();
        }

        @Override
        public void run() {

            //System.out.println("hit");

            for (int i = startPoint; i < colorArr.length; i+=4 ) {
                redTotal += ColorValue.getRed(colorArr[i]);
                blueTotal += ColorValue.getBlue(colorArr[i]);
                greenTotal += ColorValue.getGreen(colorArr[i]);

            }   

        }

        public int[] getRGBTotals() {

            int[] out = new int[3];
            out[0] = redTotal;
            out[1] = blueTotal;
            out[2] = greenTotal;

            return out;
        }
    }

}

最佳答案 是.只要数据结构在读取时不被修改,您就是安全的.启动线程之前完成的每个写入都将由启动的线程可见.

点赞