So we have a task to sort an array using semaphores in Java. The task is pretty easy and I’ve solved it in nearly couple of minutes but for the last part- merging.
Our task was to divide the given array into two, sort the parts simultaneously (using semaphores) and then merge again (using semaphores).
My question is, what is the purpose of merging it using more than one Thread as while filling the final array each of them have to constantly wait for each other, since only one can push elements to the array at one time?
Or am I missing something?
Thanks.
You can merge with two different threads from opposite sides of the resulting array.
Each thread should fill up to a half of the resulting array. then threads can use semaphores as ‘barrier’ Implementing an N process barrier using semaphores to wait each other.