![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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.
|
|
|
|
|
|
#2 |
|
Sexy Programmer
|
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.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#3 |
|
Programmer
|
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.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#5 |
|
Programmer
|
you just made me think of something that could actually work, ill try something similar...ill let you all know if it worked, thanks
|
|
|
|
|
|
#6 |
|
Programmer
|
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 |
|
|
|
|
|
#7 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#8 |
|
Programmer
|
yes, i've relized it, thanks for pointing me to the right function
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list problems | bl00dninja | C++ | 6 | Feb 17th, 2008 10:30 AM |
| singly-linked list templaste class in C++ w/ example driver | bl00dninja | Show Off Your Open Source Projects | 0 | Sep 11th, 2006 1:05 AM |
| How to get current thread ID? | myName | C++ | 2 | Jul 6th, 2006 1:52 AM |
| dev c++ software, template problem | cairo | C++ | 11 | Jun 2nd, 2006 12:42 PM |
| What is your current status? | pr0gm3r | Coder's Corner Lounge | 27 | Aug 7th, 2005 5:14 PM |