Results 1 to 5 of 5
- 11-05-2011, 06:49 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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
Last edited by Dan1979; 11-05-2011 at 06:58 AM.
- 11-05-2011, 08:38 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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:
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.Java 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 ... } } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-06-2011, 01:34 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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
- 11-06-2011, 01:37 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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...
- 11-06-2011, 11:27 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Question about 'main thread' and the thread it creates
By ggyyree in forum Threads and SynchronizationReplies: 11Last Post: 12-10-2010, 07:33 PM -
New help with simple Thread Question
By shuks in forum New To JavaReplies: 6Last Post: 01-11-2010, 02:31 AM -
Thread question
By exernet in forum New To JavaReplies: 1Last Post: 12-16-2009, 09:27 AM -
Thread question
By Moncleared in forum Advanced JavaReplies: 5Last Post: 02-09-2009, 10:33 PM -
main thread question?
By frejon26 in forum New To JavaReplies: 1Last Post: 01-24-2008, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks