Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   interrupting current execution (http://www.programmingforums.org/showthread.php?t=12477)

thenewkid Jan 30th, 2007 10:20 PM

interrupting current execution
 
hey all, for my programming course i was to write a progam that uses a combinatorial search to find a solution, the problem is of course, that as the amount of data increases so is the time to find a solution. What i want to do is to interrupt such a time taking search and go onto the next possible candidate that might produce a correct answer. Without using threads is it possible to employ a timer of some sort so when the processing takes too long it could interrupt and tell it to move on to the next candidate? This is the Knight Tour problem, i guess many of you have dealt with it one point in your lifetime. Any advice greately appreciated.

ReggaetonKing Jan 30th, 2007 10:30 PM

I would recommend looking into some more efficient ways in organizing your data. Maybe sorting your data would make the search A LOT more manageable.

Instead of having your data as just some random places just organize and use binary search.

thenewkid Jan 30th, 2007 10:43 PM

yes i know it could be done more efficiently if more advanced data structures than simple array have been used, thats not the point here, Im looking for the way to interrupt something that takes to long to arrive at the correct answer.

Arevos Jan 31st, 2007 4:46 AM

Why not just check the time at each iteration or recursion of the loop? It seems to me as if you wouldn't need threads.

thenewkid Jan 31st, 2007 10:51 AM

you just made me think of something that could actually work, ill try something similar...ill let you all know if it worked, thanks

thenewkid Jan 31st, 2007 9:36 PM

I found a place in my code where i could call elapsed() which made following code usable. Sometimes when i overthink a problem i do not see really simple solutions, I tend to think of too complicated stuff....are u like that? hehe, i mean look at this:

:

import java.util.Date;

public class myTimer {
  private final Date start;
  private Date current;
 
  public myTimer()  {
      start = new Date();
      current = null;
  }
 
  public boolean elapsed(int sec) {
      current = new Date();
      long difference = current.getTime() - start.getTime();
      if(difference >= (long)sec * 1000)
        return true;
      else return false;
  }
}


Arevos! thanks for opening my eyes lol

titaniumdecoy Jan 31st, 2007 10:24 PM

Your code creates a new Date object every time elapsed is called. You should use System.currentTimeMillis() instead.

You should also take into account the fact that performing such a check on each iteration of a loop will cost time and processing power, albeit a miniscule amount.

thenewkid Feb 2nd, 2007 8:45 PM

yes, i've relized it, thanks for pointing me to the right function


All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC