Define a job:ranjob, set to execute once per second, set does not allow overwriting concurrent execution
<BeanID= "Rankjob"class= "Com.chinacache.www.logstat.job.RankJob" /> <BeanID= "Rankjobdetail"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> < Propertyname= "TargetObject"ref= "Rankjob" /> < Propertyname= "Targetmethod"value= "Execute" /> < Propertyname= "Concurrent"value="false"/></Bean> <BeanID= "Rankjobtrigger"class= "Org.springframework.scheduling.quartz.SimpleTriggerBean"> < Propertyname= "Jobdetail"ref= "Rankjobdetail" /> <!--unit MS, half-hour 1800000 Ms - < Propertyname= "Repeatinterval"value= "/> </bean>
Java code
Public voidExecute ()throwsinterruptedexception {System.out.println ("Start Job"); Executorservice exec= Executors.newfixedthreadpool (1); Thread Thread=NewThread (NewRunnable () {@Override Public voidrun () {System.out.println ("Thread Start"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Thread End"); } }); Exec.execute (thread); Exec.shutdown (); while(!exec.isterminated ()) { //waits for all child threads to end before exiting the main thread} System.out.println ("End Job"); }
The printing results are as follows:
Start Job thread start thread end End job start Job thread start thread end End Job Start Job thread start thread end End Job
Use the isterminated () method to end the job after multiple threads have ended, and after the multi-threaded task is dispatched, use the shutdown () method to close the thread (waiting for the task to be performed and not accepting new tasks)
Define a job:ranjob, set to execute once per second, set does not allow overwriting concurrent execution
- <Bean id= "rankjob" class="Com.chinacache.www.logstat.job.RankJob" />
- <Bean id="Rankjobdetail"
- class="Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name= "targetObject" ref="rankjob" />
- <property name="Targetmethod" value="execute" />
- <property name="concurrent" value= "<span style="color: #ff0000; " ><strong>false</strong></span> " />
- </Bean>
- <Bean id= "rankjobtrigger" class="Org.springframework.scheduling.quartz.SimpleTriggerBean" >
- <property name= "jobdetail" ref="Rankjobdetail" />
- <!--unit MS, half-hour 1800000 ms---
- <property name="Repeatinterval" value= "<span style="color: #ff0000; " ><strong>1000</strong></span> " />
- </Bean>
Job Code:
- System.out.println ("Start job");
- Executorservice exec = Executors.newfixedthreadpool (1);
- Thread thread = new Thread (new Runnable () {
- @Override
- public Void Run () {
- System.out.println ("thread Start");
- try {
- Thread.Sleep (3000);
- } catch (Interruptedexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- System.out.println ("thread End");
- }
- });
- Exec.execute (thread);
- System.out.println ("End Job");
Program Output Result:
- Start Job
- End Job
- <span style="color: #ff0000;" ><strong>thread start</strong></span>
- Start Job
- End Job
- Thread Start
- Start Job
- End Job
- Thread Start
- Start Job
- End Job
- Thread Start
- <strong><span style="color: #ff0000;" >thread end</span></strong>
As you can see from the results, the concurrency override configuration for the job does not seem to be effective at all because the job is not focused on multithreaded execution
Modify the job code to add the following code at the end of the job access, the thread finishes processing the job,
- while (!exec.isterminated ()) {
- //wait for all child threads to end before exiting the main thread
- }
Program results after modifying code:
- Start Job
- Thread Start
- Thread End
You can see that the job never ends, stating that Executorservice never terminates, look at the document, add the Shutdonw () method, and the job all code is as follows:
- Public Void Execute () throws interruptedexception {
- System.out.println ("Start job");
- Executorservice exec = Executors.newfixedthreadpool (1);
- Thread thread = new Thread (new Runnable () {
- @Override
- public Void Run () {
- System.out.println ("thread Start");
- try {
- Thread.Sleep (3000);
- } catch (Interruptedexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- System.out.println ("thread End");
- }
- });
- Exec.execute (thread);
- Exec.shutdown ();
- While (!exec.isterminated ()) {
- //wait for all child threads to end before exiting the main thread
- }
- System.out.println ("End Job");
- }
The printing results are as follows:
- Start Job
- Thread Start
- Thread End
- End Job
- Start Job
- Thread Start
- Thread End
- End Job
- Start Job
- Thread Start
- Thread End
- End Job
OK, this spring quartz multithreading concurrency problem solved. In retrospect, we're going to use the isterminated () method to end the job after multiple threads are finished, and after the multi-threaded task is dispatched, use the shutdown () method to close the thread (waiting for the task to be performed, not accepting new tasks)
Spring Quartz uses multithreading concurrency "traps" (go)