2

I want to implement multiple Threads in my program. These multiple Threads should be able to process one single array.

For example:

I have an integer array:

int[] integerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Now, multiple Threads should print every item to the console, like this:

1 //Printed by Thread 1
2 //Printed by Thread 2
7 //Printed by Thread 1
8 //Printed by Thread 2
9 //Printed by Thread 3
4 //Printed by Thread 1
5 //Printed by Thread 2
6 //Printed by Thread 3
3 //Printed by Thread 3
10 //Printed by Thread 1
11 //Printed by Thread 2
12 //Printed by Thread 3

(It doesn't matter if the result is random or not.)

My solution so far was to split the array into smaller chunks. This is working, but I don't really like the solution and I don't think that this would be really thread-safe

    public static void main(String[] args) {

        int[] integerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

        int chunk = 3;
        for (int i = 0; i < integerArray.length; i += chunk) {

            int finalI = i;

            new Thread(() -> {
                int[] splittedArray = Arrays.copyOfRange(integerArray, finalI, Math.min(integerArray.length, finalI + chunk));
                for (int value : splittedArray) {
                    System.out.println(value);
                }

            }).start();
        }

    }
5
  • You do not need to split or copy the array, you can just pass the index ranges around. You could also create a Runnable for each index, and submit those to into an ExecutorService backed by a configurable number of threads. There is also parallelStream: stackoverflow.com/questions/40933362/… Commented Jun 30, 2020 at 11:31
  • 1
    What does "processing" the array involve? For example, is it read -only or does the array get modified in the process Commented Jun 30, 2020 at 11:31
  • @Thilo final and volatile are completely independent concepts, and you cannot declare a local variable volatile anyways. I'm a little confused by what you mean? Commented Jun 30, 2020 at 11:58
  • @matt: True. Volatile was non-sense here. But to make changes to variables visible to other threads (such as the ones being started here), access to the variable needs to be synchronized in some way, it does not happen automatically. Final and volatile do that already, otherwise you have to do something yourself. In the example it is okay, because the lambda will grab a copy of the (effectively final) local variable. But in general, it is something to think about before passing data between threads. Commented Jun 30, 2020 at 12:01
  • 1
    Could you explain why you don't like the solution and why you think it's not thread-safe? Then we can address those points much better than speculating in an answer or comment. Commented Jun 30, 2020 at 12:09

1 Answer 1

2

For your purpose in above given example, you do not need to split your array to pass the parts to parallel threads. Using parallel streams to process elements of an array will do it for you:

public static void main(String... args) {
    int[] integerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    Arrays.stream(integerArray)
            .parallel()
            .forEach(x -> System.out.println(Thread.currentThread().getName() + " printed value: " + x));
}

On output, you can see that ForkJoinPool-Worker-Threads will be working to print your elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.