Results 1 to 8 of 8
Thread: Forcing a thread to stop
- 06-27-2009, 11:35 AM #1
Forcing a thread to stop
Good day,
I have a problem about trying to force the specific thread to stop while it is still waiting for a specific method to return.
By convention, having a flag inside the run method and by using it to return the said method is effective, but how about "still waiting to return the invoked method inside run method"?
sample:
public void run(){
String src = instance.getData();
}
where instance.getData() method will return after several minutes of waiting. I want to force that thread to stop ( since having a flag is not effective in that scenario ). Any other alternatives on how to reproduce this one?
Anybody?
I'm looking forward to your replies :)freedom exists in the world of ideas
- 06-28-2009, 07:18 AM #2
ok, the interrupt method solves the problem(depends on how will it be implemented),
now, i want only the getData method to be stopped( while waiting for the returned value ) and not the succeeding instructions.
The alternative idea i could do is to throw that instruction in another thread and the succeeding instructions is also in another thread(waiting for a new value returned from getData through synchronized instance communication). But, i doubt to proceed hoping there's more convenient way to re-implement it. Any ideas?freedom exists in the world of ideas
- 06-30-2009, 06:53 AM #3
What is getData() doing? Interrupting the thread only creates an exception if the thread is in a blocked state, such as a socket read. If getData() is blocking, the interrupting the thread will lead to an interruped exception. You can put a try/catch around the getData() call, or you could modify getData() to catch the exception.
- 06-30-2009, 03:48 PM #4
it attempts to crawl the specific url and return its source(html)... some websites accept http connection but doesn't return any, that's why it will wait for how many minutes,hours...
So you mean, if an interrupt method invoked, the InterruptedException will be thrown from current instruction( getData )?freedom exists in the world of ideas
- 06-30-2009, 04:02 PM #5
<duplicated>
Last edited by sukatoa; 06-30-2009 at 04:36 PM. Reason: replied twice
freedom exists in the world of ideas
- 07-01-2009, 04:19 AM #6
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
This sounds like the underlying thing you want to do is set a lower timeout on the socket, then?
Neil Coffey
Javamex - Java tutorials and performance info
- 07-01-2009, 06:48 AM #7
Yes, in fact, it happens on the socket command that is waiting for a response. Neil's reply contains a good solution, if you have control over the socket buried inside getData(). You will still have to handle whatever happens when the socket times out (possibly an Exception).
If you can't modify the socket, I would suggest putting the getData() call in its own thread. I *think* you could use an inner class that implements Runnable, so that the class could easily pass the received data back to the thread starter as well as notify the thread starter that the task was complete. Have the thread starter wait for a specified time and then interrupt the thread if it has not returned. Of course, the thread starter itself will be blocked, and you will have synchronization issues to address.
- 07-17-2009, 06:41 AM #8
Hi,
I was having same problem.
you can make this thread as daemon thread. Then the control will be in parent thread. So if you want to stop this thread after a certain interval of time then you can stop this thread from the thread where this thread is invoked.Sharing knowledge means gaining more knowledge.:)
Similar Threads
-
[SOLVED] How to stop a thread
By AlejandroPe in forum New To JavaReplies: 5Last Post: 04-29-2009, 03:05 PM -
How to stop thread from being jumping off the code without executing it.....
By chiragkini in forum Threads and SynchronizationReplies: 6Last Post: 01-22-2009, 03:38 AM -
Can you stop a gif? xd
By Exhonour in forum New To JavaReplies: 0Last Post: 01-16-2009, 08:44 PM -
how to stop a thread
By willemjav in forum Advanced JavaReplies: 19Last Post: 09-10-2008, 07:11 AM -
The safe way to stop a thread
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks