Results 1 to 5 of 5
  1. #1
    Dan1979 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default 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:

    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  ...
          }
       }
    }
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dan1979 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    Dan1979 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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...

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Question about 'main thread' and the thread it creates
    By ggyyree in forum Threads and Synchronization
    Replies: 11
    Last Post: 12-10-2010, 07:33 PM
  2. New help with simple Thread Question
    By shuks in forum New To Java
    Replies: 6
    Last Post: 01-11-2010, 02:31 AM
  3. Thread question
    By exernet in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 09:27 AM
  4. Thread question
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-09-2009, 10:33 PM
  5. main thread question?
    By frejon26 in forum New To Java
    Replies: 1
    Last Post: 01-24-2008, 10:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •