Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in stop the thread  RSS feed

 
Jyotisree Dhar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I am working on webappliction , i started one thread in one jsp bean class, when i started i get the thread name and save it in database .
The problem is ,when i wanted to stop that thread from some other page by getting this thread name from database.Because the thread is running and i wanted to stop that by the name.

My question is, Is there any way to get the reference of that theard that i started earlier by enter the name and then stop it.

Please help me ,if any body have solution for that.

Regard's
Jyotisree Dhar
 
Idly Vada
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make use of Singleton design pattern to keep the references to these threads.

For example use a class ThreadController as the controller to keep track of references to these threads. Get the reference to a thread by

ThreadController.getInstance().getThread(String threadName)
 
Jeff Walker
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jyotisree,
you shouldn't be trying to stop a thread anyway. All the stop() methods in the Thread class are deprecated now. This means the stop() methods should no longer be called, from any thread. Ever!
[
Stopping a thread could cause an indeterminate state in the resource guarded by your thread, because a stopped thread immediately releases all of its locks and this means another thread could jump in and grab the locks, and there is no repeatable way for you to tell in advance which thread grabs the lock. Leading to an indeterminate state for the resource.
]
Suggest you go away and think about your design. Each thread should be written to stop itself when a certain condition is met, and thus gracefully exit by ending the run() method. I admit, at first designing your threads like this will seem hard, but you'll get used to it.
Jeff Walker
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!