Search code examples
javaarraysmultithreadingalgorithmjava-threads

Divide an uneven number between Threads


I am just learning Threads in Java and I want to sort a list of words alphabetical. My program read the words of a txt-file and put them in a String-array. The user can choose how many threads they want to use themselves. I want to split the array in even (as possible) chunks that the threads can sort by themselves.

So to my question:

How can I split the array.length as even as possible across the threads? My mind is blanking and I can't think of a smart way to do this.

e.g: If I have a array.length of 22 and 4 threads, how can give the threads in this case; 6, 6, 5 and 5 sized pieces of the array? Needs to be applicable to every number given.

I tried to explain it the best I could, please ask if something was unclear! Thank you!


Solution

  • It doesn't need to be as evenly as possible. If one thread has 6, this will determine the length of time it takes in which case it doesn't matter how many are up to 6.

    You can do

    int chunkSize = (tasks + threads - 1) / threads; // divide by threads rounded up.
    for (int t = 0; t < threads; t++) {
        int start = t * chunksSize;
        int end = Math.min(start + chunkSize, tasks);
        executor.submit(() -> {
             // inside the thread
             for (int i = start; i < end; i++) {
                 process(i);
        });
    }
    

    Note: if you use Stream.of(array).parallel() it actually create two tasks per thread. This mitigates that some batches might take longer even though they have the same number of elements.