Spring Quartz uses multithreaded concurrency "traps"

Source: Internet
Author: User

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

  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-hour 1800000 ms---
  11. <property name="Repeatinterval" value= "<span style="color: #ff0000; "  ><strong>1000</strong></span> " />
  12. </Bean>

Job 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.execute (thread);
  17. System.out.println ("End Job");

Program Output Result:

    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>

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,

    1. while (!exec.isterminated ()) {
    2. //wait for all child threads to end before exiting the main thread
    3. }

Program results after modifying code:

    1. Start Job
    2. Thread Start
    3. 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:

  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.execute (thread);
  18. Exec.shutdown ();
  19. While (!exec.isterminated ()) {
  20. //wait for all child threads to end before exiting the main thread
  21. }
  22. System.out.println ("End Job");
  23. }

The printing results are as follows:

    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, 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)

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.