1.Merge Sort (thread class with Runnable as anonymous inner class)
The merge sort algorithm is to be implemented concurrently.
Implement a public method that sorts a submitted LinkedList of a comparable content type in ascending order.
The result should be that the list passed after
the call of the method is sorted.
The code should have the following properties:
- The Thread class should be used so that parts of the program are concurrent and can be executed in parallel.
- Set a maximum number of threads depending on the number of available processors or cores.
The sorting of each individual partial list should take place in a separate thread as long as the maximum number you have specified has not yet been reached (you only need to take into account the threads you started for sorting).
If the maximum number has already been reached, no more threads may be started.
If a thread is ended, it no longer counts to the number of
started threads.
- Threads are to be created with Thread's constructor, which expects a runnable as a parameter.
- The runnable should be created with an anonymous inner class.
- Threads must be terminated by calling a public setter or mutator method of the runnable that changes a private instance variable of the boolean type.
- When sorting is complete, no threads started for sorting may be "isAlive ()".
2.Merge-Sort (Thread-Pool with Lambda)
The merge sort algorithm is to be implemented concurrently.
Implement a public method that sorts a submitted LinkedList of a comparable content type in ascending order.
The result should be that the passed list is sorted after calling the method.
The code should have the following properties:
- A thread pool is to be used so that parts of the program are concurrent and can be executed in parallel.
- Select a thread pool type suitable for the task and create it.
- The sorting of each individual partial list should happen separately in the thread pool.
- The tasks that are processed concurrently in the thread pool should be transferred in the form of lambda expressions.
- The thread pool must be ended at the end of the sorting and its resources released again.
please note that the implementation is based on the following sequential merge sort!
For sorting :
public void sort(LinkedList dataToBeSorted) {
if (dataToBeSorted.size()
return;
int mid = dataToBeSorted.size()/2;
LinkedList left = (LinkedList) dataToBeSorted.subList(0, mid);
LinkedList right = new LinkedList<>(dataToBeSorted.subList(mid, dataToBeSorted.size()));
sort(left);
sort(right);
merge(left, right, dataToBeSorted);
}
for merging :
private void merge(LinkedList left, LinkedList right, LinkedList dataToBeSorted) {
int leftIndex, listIndex,rightIndex;
leftIndex=rightIndex=listIndex=0;
while (leftIndex
if ( ((Comparable)left.get(leftIndex)).compareTo(right.get(rightIndex))
dataToBeSorted.set(listIndex++, left.get(leftIndex++));
else
dataToBeSorted.set(listIndex++, right.get(rightIndex++));
}
while (leftIndex
dataToBeSorted.set(listIndex++, left.get(leftIndex++));
while (rightIndex
dataToBeSorted.set(listIndex++, right.get(rightIndex++));
}