• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to close a Socket from an Executor?

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic