Reputation: 23
I'm writing an analysis of standard Maze Solving Algorithms and neural network based Maze Solving algorithms. I have created 7 classes (for now), one for each maze solving algorithm. In my main class, I initialize each of the algorithm objects, create a thread for each one, and make them solve a generated maze. Each of the algorithm's is basically comprised of one main loop and inner parts. At the end of each of the loops, I have a code segment that logs information needed for my analysis (time, number of steps, number of turns, etc.). After each of the algorithm threads has completed, I call a function which takes each of the algorithm objects and creates a png of the solved maze. Then I simply load this picture in a window.
This works fine, but for a live demonstration I would like the image to instead update for each loop cycle so that I could show the maze being solved in real time. For this to happen, I need each of the threads to stop in their loop at the part where they log the information until the main thread calls the function that creates the png. I'm not really sure how to make this communication happen, as all the algorithms complete a loop cycle at different times.
This is the code I have so far in my main function:
DepthFirst generator = new DepthFirst(30); // Generates the maze to be solved
byte[][] hi = generator.generate();
JFrame frame = new JFrame("DepthFirstTrial0"); // Creates the window to
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // display the finished maze
MazeSolver mazes[] = new MazeSolver[7]; // Creates each of the algorithms
mazes[0] = new RandomMouse(hi); // They are all subclasses of MazeSolver
mazes[1] = new LeftHandFollower(hi);
mazes[2] = new RightHandFollower(hi);
mazes[3] = new Pledge(hi);
mazes[4] = new Tremaux(hi);
mazes[5] = new RecursiveBacktracker(hi);
mazes[6] = new DeadEndFiller(hi);
Thread mazeThreads[] = new Thread[7];
for(int i = 0; i < 7; i++) {
mazeThreads[i] = new Thread(mazes[i]); // Puts each one in a threaad
}
for(int i = 0; i < 7; i++) {
mazeThreads[i].start(); // Starts each thread
}
Thread.sleep(1000) // Waits at most one second for them to finish
MazeSolver.toPng(mazes, 0, "DepthFirst"); // Creates the png
frame.getContentPane().add(new MazeAlgorithmAnalyzer.loadImage()); // Loads into the window
frame.pack();
frame.setVisible(true);
Any help would be greatly appreciated. Thanks in advance! Ashwin
Upvotes: 2
Views: 942
Reputation: 5326
You want a CyclicBarrier.
Inside each thread for each maze solver, after each step you just add the line
cyclicBarrier.await();
They will wait at that line until all other threads are at that point too and then they all start again together.
Upvotes: 2
Reputation: 3379
I must admit that I battled to understand your exact requirement but if you are trying to achieve basic inter-thread communication then you can probably accomplish this through wait(), notify() and notifyAll(). Here are two tutorials you can look at for these methods: Here and here.
Upvotes: 0
Reputation: 17444
Use SwingWorker
. Have your maze solving algortihm invoke the publish()
method each time an intermediate result is there. Some time thereafter, the process()
method is called on the EDT where you can update the display.
Upvotes: 2