Java threading. Am I writing thread safe code?

I'm currently trying to switch to parallel processing, so for this I am writing a program that processes the image, giving information about its color values ​​in general. I am doing several tests on this one class with a randomly generated array of integers and 4 threads being executed to process every 4th pixel from their respective source places. I'm just wondering if this reading is thread safe? Can multiple threads read the same data structure if that is what I want?

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;
        }
    }

}
+4
source share
3 answers

. , . , , ​​.

+2

, , . , , Fork-Join framework , , .

0

:

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

colorArr - ; Runnable , .

, , , , , . " " , colorArr[i] , , , colorArr. , . :

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

, , colorArr , . synchronized, colorArr , synchronized, , - , , . , , concurrency.

0

All Articles