• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to kill/stop a Thread

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to all,

In my application, using RMI and sockets, when an user logs in it creates an thread corresponding to the username. Now when user logs out I need to kill/stop the thread corresponding to that user.

How can I identify the thread corresponding to the user and how can I stop that thread?

Thanks in Advance.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

rajshkhr pandey wrote: How can I identify the thread corresponding to the user ...


Well, you could define e.g. a HashMap (username, created thread).
I guess this would be the easiest way ...

rajshkhr pandey wrote:... and how can I stop that thread?


You will have to declare some variable (usually a boolean), which is then checked in a loop (within a run method).
Beside this, you may also want to call an interrupt() ... just in case the thread is currently waiting or is blocked.

Hope this helps.


Regards,
Rok
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to amplify what Rok said - you have to design your run method so that it can be exited gracefully. There are many pitfalls if you try to just arbitrarily stop a Thread. Read the java.lang.Thread documentation carefully, especially the deprecated stop() method documentation.

If the user is formally logging out rather then just disconnecting, you already have the Thread corresponding to the user so clean up of resources and a graceful exit from run should be easy with a proper architecture.

Bill
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic