Thread question / dovetailing in Java
I have a function in Java let's say:
public boolean algorithm (int ...) {
...
boolean solFound = false;
has a while (counter < 100000 && solFound == false){
...
if("solution is found"){
solFound = true;
break;
..
}
}
in the while loop I am looking for the solution starting from a randomly seed. I would like to write a program that runs 5 threads of this algorithm and when any of the threads finds a solution, all the threads will stop and the program will end. I think this is called dovetailing. The reason why I took this approach is that the algorithm that I am running if it starts for some "bad"/not that good seeds it may take too much time to find a solution....
This is prob a simple question, but any help is appreciated.
Thank you,
-- Dan :s:
Re: Thread question / dovetailing in Java
The Runnable that is run by the Thread should be stoppable from 'outside' of the Thread; is is easy to implement:
Code:
class YourRunnable implements Runnable {
private boolean stop;
public void stop() { stop= true; }
// interface implementation:
public void run() {
while (!stop && counter < 100000 && solFound == false){
// your algorithm here ...
}
}
}
A 'controller' Thread fires up the solving Threads with the above Runnables and when one of them reported back a solution, this 'controller' Thread stops the other running Runnables.
kind regards,
Jos
Re: Thread question / dovetailing in Java
So, this is my Runnable class:
public class ThreadAlg implements Runnable {
private int[][] mat;
private int l;
private boolean stop = false;
public ThreadAlg(int[][] mat, int l) {
super();
this.mat = mat;
this.l = l;
}
public void stop() {
stop = true;
}
public void run() {
while (!stop) {
stop = Algorithm_1.algorithm(mat, l);
}
}
}
------
And this is where I run the alg from:
public static void main(String[] args) {
int[][] mat = SasaoFunGenerator.sasaFun(50);
int noVariables = 8;
int noThreads = 3;
Date date = new Date();
ThreadAlg[] tAlg = new ThreadAlg[noThreads];
Thread[] thread = new Thread[noThreads];
for (int i = 0; i < noThreads; i++) {
tAlg[i] = new ThreadAlg(mat, noVariables);
thread[i] = new Thread(tAlg[i]);
thread[i].start();
}
System.out.println("Time: " + (new Date().getTime() - date.getTime()) + " ms");
}
------
Algorithm_1.algorithm(mat, l): is the function where the algorithm works and returns a boolean. I am pretty sure that I should not have in my run() function:
while (!stop) {
stop = Algorithm_1.algorithm(mat, l);
}
probably it should be:
while (!stop) {
Algorithm_1.algorithm(mat, l);
}
but neither works. I have experience with Java, but not with threads at all. Jos, let me know if you see that I am doing something completely wrong ... I am eager to learn.
Also, if you can point to me to some threading tutorials (not the sun ones) it would be great.
I tried to put the algorithm where told me to ... but still does not work.
Thank you a LOT,
-- Dan
Re: Thread question / dovetailing in Java
I also tried this:
ThreadAlg[] tAlg = new ThreadAlg[noThreads];
for (int i = 0; i < noThreads; i++) {
tAlg[i] = new ThreadAlg(mat, noVariables);
tAlg[i].run();
}
in my main, but still no success...:s:
Re: Thread question / dovetailing in Java
You start all those Threads but nowhere (when one of them is ready) are those Threads stopped by stopping the (other) Runnables ...
kind regards,
Jos