This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to close a Socket from an Executor?  RSS feed

 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently experimenting a networked application that works like this. A server creates a ServerSocket object and wait for clients to connect in an infinite loop. Once a client is accepted via the accept() method (and thus I have a Socket object now), I pass this Socket object into a Client object that implements Runnable, and in the run() method, it simply waits for messages from the client (by using a BufferedReader object, for instance).

I wish to take advantage of the new concurrency package of Java 1.5. I was thinking of passing the Client object into an ExecutorService object. The problem lies here: if I wish to shutdown the ExecutorService object using the shutdownNow() method, I want to interrupt the BufferedReader object of the Client object (most likely the BufferedReader is now blocked due to waiting for messages). I must perform the interruption which will result in an IOException, and subsequently close the Socket, otherwise the thread doesn't die. Is there any way to do this?
[ March 11, 2005: Message edited by: Liang Anmian ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could extend Thread and override interrupt() to do something to the internal "target" runnable before doing its normal thing. That sounds kinda scary to me. What do you think?
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but do you mean that I should implement my own ThreadFactory? If yes, it doesn't work this way, because the Runnable object being passed into the newThread() method is not the original Runnable object being passed into the ExecutorService, but a custom Worker object implemented by the developers (which is a private class). So I can't do anything to the original Runnable object from the Thread object.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, right you are. The target in the thread will be the worker. Bummer.

My ideas here get worse and worse. Your tasks could register themselves somewhere whent they start, deregister when they end. At shutdown time anything on the registered list is currently running and you can try to kill it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!