Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can one thread stop another?  RSS feed

 
Sayantan Auddy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The situation I'm facing is this:

There are two threads t1 and t2.
t2 should wait a certain amount of time for t1 for finishing its job,but if the time limit is up t2 should execute it's run method and should make t1 stop. The first part (waiting a certain amount of time for t1) can be achieved by the join(long millis) method but how can t2 make t1 stop?? any suggestions?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual way to stop a thread is to set some boolean variable that gets checked periodically by the thread to be stopped.
 
Sayantan Auddy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that t1 is actually waiting for some response from a telnet server. The program gets blocked if no reply comes. I want to wait for only a certain time and if no reply comes I want to stop the thread waiting for the reply (t1). As inside t1's run() method there is no repeating code (as inside a loop) I can't check on some boolean variable either to stop t1. Here is the body of t1's run method.



The code works fine if all goes well,but suppose the server is down and the expected response never comes. The program blocks indefinitely in such a scenario.
That is why I thought that I would have the readUntil() and write() methods in thread t1's run method and have another thread t2 which would wait for a certain time for t1 to finish and when the time expires it should stop t1 and write an entry in a log file saying that no response was received from the server.
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally your readUntil methods should take another param or something that tells it to wait for read for only so much time only.

But, if that is not possible:
How about calling stop() method on t1, although it is deprecated and not recommended.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't use Thread.stop; it is deprecated for good reasons.

Does the Telnet library you're using support timeouts? For example, the Telnet client in Apache Common Net does support them.
 
Sayantan Auddy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using apache commons net.
I am new to this, so please could you show me how to time out a session?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The TelnetClient.setSoTimeout method may be helpful.
 
Sayantan Auddy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Ulf. This solves my problem
Thank you James for the suggestion.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using an interruptible channel of communication,available in the java.nio,in T1,and have it interrupted or asynchronously closed by T2?

http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/InterruptibleChannel.html

 
Sayantan Auddy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ajay.Thats another way of doing it.Will try it out
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!