• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Ending a thread from jsp

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am having a problem using Timer and the underlaying TimerThread class.

I'm using a Timer to schedule a routine testing class that runs once every day to test output of the server. The timer is started from a JSP page and can be stopped from the same page.

The problems I'm facing are:
1. When I append the stop=1 as parameter to my JSP URL, the instance of Task class is not the same as the Task instance when I start the timer. Therefore I cannot stop the timer using this method.
2. I can iterate through the Thread in the server to find the underlaying TimerThread invoke by the Timer class. But there is no way to cast it to either Task or Timer class in order to stop the timer. TimerThread is an inner class in Timer, and there's no way for me to set the variables within this class.
3. Is it okay to stop the thread using thread.stop() since I cannot get the class instance to use the volatile variable method.

Currently, I'm considering stopping it using thread.stop() but since this method is deprecated and unsafe, is there any other safer and better methods to use?

The scriptlet in my JSP page to start the timer.



My TaskTimer Class

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time you accesses the JSP it will create a new instance of the "TaskTimer" class so you don't have access to the one(s) created previously. In order to have one and only one instance you may need to look for the Singleton Pattern
 
Lilian Chase
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijitha,

Thank you so much for the reply! Using the singleton pattern, I'm able to get the same instance and stop the timer.

Thank you so much!

Another question: Is using Timer or ScheduledExecutorService a better choice?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would go with the latest one
 
Get out of my mind! Look! A tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic