Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

By Day 7 , write a 2- to 3-page paper evaluating the implementation of the sort

ID: 667149 • Letter: B

Question

By Day 7, write a 2- to 3-page paper evaluating the implementation of the sort algorithm. Answer the following questions:

Is the threaded implementation correct, or are there data integrity concerns due to concurrency control?

If your implementation is correct, what protections are in place to ensure atomicity and consistency? Could a more effective method of ensuring data integrity be implemented in your solution?

If you ran into trouble with your implementation, provide specifics about the data integrity problems you encountered and explain the modifications that will be necessary to correct the data integrity and concurrency issues.

Include in your analysis an explanation of the operation of the threaded implementation, and if there are data integrity issues, suggest one or more modifications to the implementation to resolve these concerns

Explanation / Answer

Here is program for sorting using thread...

import java.util.ArrayList;
import java.util.List;

public class ThreadedMergeSorter<E extends Comparable<? super E>> implements, Runnable
{
private List<E> list;
private List<E> left, right;
private Thread t1, t2;
private static final int maxThreads = 4;
private static AtomicInteger currentThreads = new AtomicInteger(0);

private ThreadedMergeSorter(List<E> list)
{
this.list = list;
}

public ThreadedMergeSorter(){}


public void sort(List<E> list)
{
if(list.size() > 1)
{
left = new ArrayList<E>(list.subList(0, list.size()/2));
right = new ArrayList<E>(list.subList(list.size()/2, list.size()));

list.clear();

if(currentThreads.get() < maxThreads)
{
t1 = new Thread(new ThreadedMergeSorter<E>(left));
t1.start();
currentThreads.incrementAndGet();
}
else sort(left);

if(currentThreads.get() < maxThreads)
{
t2 = new Thread(new ThreadedMergeSorter<E>(right));
t2.start();
currentThreads.incrementAndGet();
}
else sort(right);

try{
if(t1 != null)
{
t1.join();
currentThreads.decrementAndGet();
}
if(t2 != null)
{
t2.join();
currentThreads.decrementAndGet();
}
}catch(InterruptedException e){}

list.addAll(mergeSortedLists(left, right));
}
}

private List<E> mergeSortedLists(List<E> leftList, List<E> rightList)
{
ArrayList<E> list = new ArrayList<E>();

while(!leftList.isEmpty() && !rightList.isEmpty())
{
if((leftList.get(0)).compareTo(rightList.get(0)) <= 0)
list.add(leftList.remove(0));
else
list.add(rightList.remove(0));
}

if(!leftList.isEmpty())
list.addAll(leftList);
if(!rightList.isEmpty())
list.addAll(rightList);

return list;
}


@Override
public void run()
{
sort(this.list);
}
}


The problem is in the sort(List<E> list) method by the if statements and the try catch block.

Solution:

First off, I am not running anything in parallel. Threads are started with start(), not run(), which simply calls the run method on the current thread.

Second, if I have shared variables being updated, try to declare them as AtomicInteger like :

private static AtomicInteger currentThreads = new AtomicInteger(0);

Then use these methods to atomically increment/decrement:

currentThreads.incrementAndGet();
currentThreads.decrementAndGet();