multithreading - stop a thread in java after a given time - doesn't work -
i have complex function (optimisation) can potentially enter in loop or take time, , time allowed set user.
therefore trying make run function in separate thread, , stop if maximum time passed. use code similar 1 below, doesn't work,
int timemax = 2; //time in minutes thread thread_object = new thread_class(... args...); try { thread_object.start(); thread_object.join(timemax*60*1000); }
i think i'm not using function "join" properly, or doesn't have understood. idea?
thanks!
thanks answers, have found better idea here*. works still uses function "stop" deprecated. new code is:
thread thread_object = new thread_class(... args...); try { int timemax = 1; thread_object.start(); thread.currentthread().sleep( timemax * 1000 ); if ( thread_object.isalive() ) { thread_object.stop(); thread_object.join(); } } catch (interruptedexception e) { }
not yet sure of function of "join", i'll have go have @ book.
i suggest use timer.
Comments
Post a Comment