Spring quartz uses multi-thread concurrency "trap"
Blog type:
Multi-thread quartzspringthreadbean
Define a job: ranjob, set to run once per second, set to not allow overwrite concurrent execution
XML Code
- <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 an hour 1800000 MS -->
- <Property name = "repeatinterval" value = "<span style =" color: # ff0000; "> <strong> 1000 </strong> </span>"/>
- </Bean>
Job Code:
Java 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.exe cute (thread );
- System. Out. println ("End Job ");
Program output result:
Java code
- 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>
The result shows that the concurrent overwrite configuration of the job does not take effect at all because the job does not focus on multi-thread execution.
Modify the job code and add the following code. At the end of the job access, the thread finishes processing the job,
Java code
- While (! Exec. isterminated ()){
- // Exit the main thread only after all sub-threads are finished.
- }
After the code is modified, the program result is as follows:
Java code
- Start Job
- Thread start
- Thread end
We can see that the job has never ended, indicating that the executorservice has never ended. Check the document and add the shutdonw () method. All the code of the job is as follows:
Java code
- 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.exe cute (thread );
- Exec. Shutdown ();
- While (! Exec. isterminated ()){
- // Exit the main thread only after all sub-threads are finished.
- }
- System. Out. println ("End Job ");
- }
The output is as follows:
Java code
- 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. Now, the multi-thread concurrency problem of spring quartz is solved. Review, we need to use the isterminated () method and other threads to end the job. After the multi-thread task is distributed, we need to use the shutdown () method to close the thread in sequence (waiting for the task to be executed, do not accept new tasks)