
06-27-2009, 12:35 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
|
|
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
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
|
|

06-28-2009, 08:18 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
|
|
|
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?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
|
|

06-30-2009, 07:53 AM
|
 |
Senior Member
|
|
Join Date: Dec 2008
Posts: 972
Rep Power: 2
|
|
|
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, 04:48 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
|
|
|
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 )?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
|
|

06-30-2009, 05:02 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
|
|
|
<duplicated>
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
Last edited by sukatoa; 06-30-2009 at 05:36 PM.
Reason: replied twice
|
|

07-01-2009, 05:19 AM
|
|
Senior Member
|
|
Join Date: Nov 2008
Posts: 275
Rep Power: 2
|
|
|
This sounds like the underlying thing you want to do is set a lower timeout on the socket, then?
|
|

07-01-2009, 07:48 AM
|
 |
Senior Member
|
|
Join Date: Dec 2008
Posts: 972
Rep Power: 2
|
|
|
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, 07:41 AM
|
 |
Member
|
|
Join Date: Apr 2009
Location: Gurgaon,India
Posts: 24
Rep Power: 0
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 09:26 PM.
|
|