0

I know this has been asked a few times before:

Java: how to synchronize array accesses and what are the limitations on what goes in a synchronized condition

Synchronizing elements in an array

but I couldn't quite find the answer to my question: When I have an array with multiple elements and my threads shall modify these elements at the same time (otherwise there would be no advantage of using threads, right?) and I use my array as a lock (which kind of makes sense because it is the critical data which could go into inconsistent state due to time-slicing) then my operations on the array are safe BUT all the parallelization would be lost, wouldn't it?!

I add some code I want to try this on:

import java.util.Arrays;

public class ParallelNine extends Thread{

    public static final int[] input = new int[]{
        119_119_119,
        119_119_119,
        111_111_111,
        999_999_999,
    };

    private static int completed = 0;

    static void process(int currentIndex){
        System.out.println("Processing " + currentIndex);
        String number = Integer.toString(input[currentIndex]);
        int counter = 0;
        for(int index = 0; index < number.length(); index++){
            if(number.charAt(index) == '9')
                counter++;
        }
        input[currentIndex] = counter;
    }

    @Override public void run(){
        while(completed < input.length){
            synchronized(input){
                process(completed);
                completed++;
            }
        }
    }

    public static void main(final String... args) throws InterruptedException{
        Thread[] threads = new Thread[]{new ParallelNine(), new ParallelNine(), new ParallelNine(), new ParallelNine()};
        for(final Thread next : threads){
            next.run();
        }
        for(final Thread next : threads)
            next.join();
        System.out.println(Arrays.toString(input));
    }
}

We have an array with primitive int values. A dedicated method (process) counts, how often the number 9 appears in the integers value at an index of our array and then overwrites the checked arrays element with the counted number of nines. So the correct output would be [3, 3, 0, 9].This array (input) is pretty small but if we have a few thousand entries for example it would make sense to have multiple threads counting nines: So I synchronized on the array but as mentioned above: All the parallelization is lost because only ONE thread at a time has access to the array!

Community
  • 1
  • 1
kimsay
  • 311
  • 3
  • 15
  • 1
    When they access different index elements, they are threadsafe. – huseyin tugrul buyukisik Jul 05 '15 at 10:27
  • don't synchronize. if your threads have their own driving lanes then locking is unnecessary. So far as I see, no thread actually touches anything another thread does. – Catalyst Jul 05 '15 at 10:27
  • @huseyintugrulbuyukisik actually they could access the same index array when one thread gets stuck in the process method (time-slicing). So it would not increment completed and another thread would access the same element as the thread before. – kimsay Jul 05 '15 at 10:35
  • @Catalyst The threads could work on the same index, couldn't they? – kimsay Jul 05 '15 at 10:39
  • Just noticed that, don't do that. just pass the index to operate on to each runnable. If you had a lot of data items (say 1000) you just pass the beginning of each block to each thread. – Catalyst Jul 05 '15 at 10:41
  • @Catalyst ok, I got the idea behind it. But how do I tell each thread in what range it is allowed to operate? Eg when I have two threads: Thread one is working on index 0-1 and thread two on 2-3. – kimsay Jul 05 '15 at 10:59
  • take a look at http://stackoverflow.com/questions/877096/how-can-i-pass-a-parameter-to-a-java-thread – Catalyst Jul 05 '15 at 20:46

0 Answers0