2013年9月6日星期五

java timer stop the problem ? ? Neighborhoods

jsp + servlet implementation at the beginning and end of the page, click the button , the servlet can start a timer task . servlet key code as follows
String value = request.getParameter ("value"). trim (); jsp mass participation , as open or close;
System.out.println ("__value" + value);
final Timer t1 = new Timer (); instantiated timer
if (value.equals ("open")) { for the open task execution mytask
t1.schedule (new MyTask (), 1000,5000);}
if (value.equals ("close")) {/ / This is used to stop this task
try {
Thread.sleep (2000);
t1.cancel ();
} catch (InterruptedException e) {
e.printStackTrace ();
} Click End
Why not terminate it ? Click Start Click again and again at the end of a timer thread instantiates it ? Then how it all ends ? Or is it because ?
heroes who ask , thanks ! No points , sorry
------ Solution ---------------------------------- ----------
landlord you carefully analyze your code will know that you have to go up each request a new timer instance , when you start the timer after the end of the request this instance ran when you issue the end , when they request a new instance of this timer is not your timer that started timer. So you can not stop the original event .
To stop the original launch event you can put Timer timer = new Timer (); as a servlet member variables , post or get method being would not use this directly in the new timer on the line. Because the servlet is a service to multiple requests , you can ensure that your timer is the same object
------ For reference only ----------------- ----------------------
I know that might be the problem here , just do not know how to solve . Thanks
------ For reference only ------------------------------------ ---
another in to ask if it is normal java class how to do it ? Global variables is not feasible ah ?
------ For reference only -------------------------------------- -

web request how is it normal java class , unless you are using a framework such as struts2 . I do not know what you say in general what kind of java classes

没有评论:

发表评论