rendered paste bodyclass PathFinderThread extends Thread { private Labyrinth myLab; private Position myPos; private static boolean logged = false; private int myNumber; public static int counter = 0; public PathFinderThread(Labyrinth labyrinth, Position start) { myLab = labyrinth; myPos = start; } public void run() { /* * log starting thread */ if (logged) { myNumber = ++counter; System.out.println("new Thread No. " + myNumber + ":"); System.out.println("Thread No. " + myNumber + " starts at [" + myPos.x + ", " + myPos.y + "]"); } /* * find out positions in neighborhood */ Position[] newPos = new Position[4]; newPos[0] = new Position(myPos.x - 1, myPos.y); newPos[1] = new Position(myPos.x + 1, myPos.y); newPos[2] = new Position(myPos.x, myPos.y - 1); newPos[3] = new Position(myPos.x, myPos.y + 1); /* * check all four directions */ for (int i = 0; i < newPos.length; i++) { if (myLab.isInsideOfLabyrinth(newPos[i])) { switch (myLab.getElementAtPos(newPos[i])) { /* * if neighbor field is free, start thread */ case Labyrinth.FREI: myLab.setElementAtPos(myPos, Labyrinth.WEG); PathFinderThread newThread = new PathFinderThread(myLab, newPos[i]); newThread.start(); try { newThread.join(); } catch (Exception ex) { System.err.println("Interrupted Exception"); System.exit(-1); } break; /* * if neighbor field is the exit, search should terminate ;) */ case Labyrinth.AUSGANG: myLab.setElementAtPos(myPos, Labyrinth.WEG); if (logged) { System.out.println("found by thread " + myNumber); } myLab.print(); System.exit(0); break; } } } /* * print out end of thread */ if (logged) { System.out.println("Thread No. " + myNumber + " ends at [" + myPos.x + ", " + myPos.y + "]"); } /* * delete waypoint */ myLab.setElementAtPos(myPos, Labyrinth.BESUCHT); } /** * Switches logging mechanism on */ public static void loggingOn() { logged = true; }}