Spring quartz uses multi-thread concurrency "trap"

Source: Internet
Author: User
Spring quartz uses multi-thread concurrency "trap"

Blog type:

  • Java
Multi-thread quartzspringthreadbean

Define a job: ranjob, set to run once per second, set to not allow overwrite concurrent execution

 

XML Code  
  1. <Bean id = "rankjob" class = "com. chinacache. www. logstat. Job. rankjob"/>
  2. <Bean id = "rankjobdetail"
  3. Class = "org. springframework. Scheduling. Quartz. methodinvokingjobdetailfactorybean">
  4. <Property name = "targetobject" ref = "rankjob"/>
  5. <Property name = "targetmethod" value = "execute"/>
  6. <Property name = "concurrent" value = "<span style =" color: # ff0000; "> <strong> false </strong> </span>"/>
  7. </Bean>
  8. <Bean id = "rankjobtrigger" class = "org. springframework. Scheduling. Quartz. simpletriggerbean">
  9. <Property name = "jobdetail" ref = "rankjobdetail"/>
  10. <! -- Unit MS, half an hour 1800000 MS -->
  11. <Property name = "repeatinterval" value = "<span style =" color: # ff0000; "> <strong> 1000 </strong> </span>"/>
  12. </Bean>

 

Job Code:

Java code  
  1. System. Out. println ("Start Job ");
  2. Executorservice exec = executors. newfixedthreadpool (1 );
  3. Thread thread = new thread (New runnable (){
  4. @ Override
  5. Public void run (){
  6. System. Out. println ("thread start ");
  7. Try {
  8. Thread. Sleep (3000 );
  9. } Catch (interruptedexception e ){
  10. // Todo auto-generated Catch Block
  11. E. printstacktrace ();
  12. }
  13. System. Out. println ("thread end ");
  14. }
  15. });
  16. Exec.exe cute (thread );
  17. System. Out. println ("End Job ");

 

Program output result:

Java code  
  1. Start Job
  2. End Job
  3. <Span style = "color: # ff0000;"> <strong> thread start </strong> </span>
  4. Start Job
  5. End Job
  6. Thread start
  7. Start Job
  8. End Job
  9. Thread start
  10. Start Job
  11. End Job
  12. Thread start
  13. <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  
  1. While (! Exec. isterminated ()){
  2. // Exit the main thread only after all sub-threads are finished.
  3. }

 

After the code is modified, the program result is as follows:

Java code  
  1. Start Job
  2. Thread start
  3. 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  
  1. Public void execute () throws interruptedexception {
  2. System. Out. println ("Start Job ");
  3. Executorservice exec = executors. newfixedthreadpool (1 );
  4. Thread thread = new thread (New runnable (){
  5. @ Override
  6. Public void run (){
  7. System. Out. println ("thread start ");
  8. Try {
  9. Thread. Sleep (3000 );
  10. } Catch (interruptedexception e ){
  11. // Todo auto-generated Catch Block
  12. E. printstacktrace ();
  13. }
  14. System. Out. println ("thread end ");
  15. }
  16. });
  17. Exec.exe cute (thread );
  18. Exec. Shutdown ();
  19. While (! Exec. isterminated ()){
  20. // Exit the main thread only after all sub-threads are finished.
  21. }
  22. System. Out. println ("End Job ");
  23. }

 

The output is as follows:

 

Java code  
  1. Start Job
  2. Thread start
  3. Thread end
  4. End Job
  5. Start Job
  6. Thread start
  7. Thread end
  8. End Job
  9. Start Job
  10. Thread start
  11. Thread end
  12. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.