Untitled
unknown
plain_text
4 years ago
2.9 kB
13
Indexable
import java.util.ArrayList;
public class CheckThread implements Runnable {
private int x;
private ArrayList<CheckThread> arrayList;
private Monitor monitor;
private int index;
public CheckThread(int x, Monitor monitor, ArrayList<CheckThread> arrayList,int index) {
this.x = x;
this.monitor = monitor;
this.arrayList = arrayList;
this.index = index;
}
public int getX()
{
return x;
}
@Override
public void run() {
for( int i=0 ; i<monitor.getChecks(); i++) {
try {
System.out.println(("check " + i + "INDEX" + index));
int result = checkNeighbours();
monitor.setBool(index);
isDone(result,index);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void isDone(int res, int index) throws InterruptedException {
System.out.println("ISDONE?");
if (monitor.isFinished()) {
System.out.println(arrayList);
monitor.setBooleansFalse();
System.out.println("Before Notify of Index " + index);
notifyAll();
System.out.println("After Notif of index " + index);
}
else {
System.out.println("Wait of Thread " + index);
wait();
}
System.out.println("Increase Decrease INDEX " + index);
this.x+= res;
}
public synchronized int checkNeighbours() {
int [] indexes = setNeighbours(index);
int pre = indexes[0];
int next = indexes[1];
if(isSmall(index,pre,next))
return 1;
if (isBig(index,pre,next))
return -1;
return 0;
}
@Override
public String toString() {
return Integer.toString(x);
}
public boolean isBig(int index, int pre, int next)
{
return arrayList.get(index).getX() > arrayList.get(pre).getX() && arrayList.get(index).getX() > arrayList.get(next).getX();
}
public boolean isSmall(int index, int pre, int next)
{
return arrayList.get(index).getX() < arrayList.get(pre).getX() && arrayList.get(index).getX() < arrayList.get(next).getX();
}
public int [] setNeighbours(int index)
{
int [] returnVal = new int[2];
if ( index ==0 )
{
returnVal[0] = arrayList.size() -1;
returnVal[1] = index +1;
}
else if(index == arrayList.size() -1)
{
returnVal[0] = index -1;
returnVal[1] = 0;
}
else
{
returnVal[0] = index -1;
returnVal[1] = index +1;
}
return returnVal;
}
}
Editor is loading...