Reference: "Java timer timer.schedule perform tasks and timings in the Web" "Java" timer and TimerTask detailed "
Do a demo to complete the following functions:
Having tomcat start a thread pool and a timer thread at the start of the Web program, the timer thread runs once at a time (for example, checking for expired users, like polling), and then triggers another background thread (thrown to the thread pool for processing, such as processing expired users, sending, etc.).
Speak directly in code
Staruplistener.java
Package Listeners;
Import Java.util.Timer;
Import java.util.concurrent.Executors;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
Import Backstage.mytimertask;
Import Support.poolmanager;
@Override
Sce.getservletcontext (). log (" timer Destruction");
@Override
Sce.getservletcontext (). log (" boot thread Pool");
Start a thread pool to deal with different task;
Poolmanager.pool = Executors.newfixedthreadpool (10);
Sce.getservletcontext (). log (" start Timer");
Timer timer=New timer (true);
Timer.schedule (
Sce.getservletcontext (). log ("The task schedule has been added");
}
Mytimetask.java
Package BackStage;
Import Java.util.TimerTask;
Import Javax.servlet.ServletContext;
Import Support.poolmanager;
Null
int param;
This.context = context;
@Override
Context.log (" start execution of specified tasks");
System.err.println (" run every 10 seconds!") I'm going to call the thread pool to perform another task ");
PoolManager.pool.execute (new Workthread (param++));
Context.log (" specified task execution end");
}
Poolmanager.java
Package support;
Import Java.util.concurrent.ExecutorService;
static Executorservice pool;
Package BackStage;
int param;
This.param = param;
@Override
TODO do something
System.err.println (" I am called by the thread pool to execute!") Parameters: "+ param);
}
Web. XML fragment
<listener>
listener-class>listeners.startuplistener</listener-class>
Listener>
Output: (I like to use system.err output red!) More festive ╮(╯▽╰)╭)
Info: Start execution of specified scheduled tasks
Claiming to run every 10 seconds Oh! I'm going to call the thread pool to perform another task.
2011-3-31 18:26:46 Org.apache.catalina.core.ApplicationContext Log
Info: Specify the end of the scheduled task execution
I was called by the thread pool to execute! Parameters: 1
2011-3-31 18:26:56 Org.apache.catalina.core.ApplicationContext Log
Info: Start execution of specified scheduled tasks
Claiming to run every 10 seconds Oh! I'm going to call the thread pool to perform another task.
I was called by the thread pool to execute! Parameters: 2
2011-3-31 18:26:56 Org.apache.catalina.core.ApplicationContext Log
Info: Specify the end of the scheduled task execution
2011-3-31 18:27:06 Org.apache.catalina.core.ApplicationContext Log
Info: Start execution of specified scheduled tasks
Claiming to run every 10 seconds Oh! I'm going to call the thread pool to perform another task.
2011-3-31 18:27:06 Org.apache.catalina.core.ApplicationContext Log
Info: Specify the end of the scheduled task execution
I was called by the thread pool to execute! Parameters: 3
Programs that are executed at the Java Web App startup interval