How Java determines if all the thread pool tasks are done

Source: Internet
Author: User

Shutdown
void Shutdown ()
    • Starts a sequential shutdown, performs a previously submitted task, but does not accept new tasks. If it is already closed, the call has no other effect.



        • Thrown:

        • SecurityException-If the security manager exists and shuts down, this executorservice may manipulate some threads that do not allow the caller to modify (because it is not persisted RuntimePermission ("Modifythread")), or the security manager's CheckAccess method denies access.


    isterminated
    Boolean isterminated ()
      • Returns true if all tasks have completed after closing. Note that isterminated will never be true unless you first call shutdown or Shutdownnow.



          • Return:

          • Returns true if all tasks are completed after closing

       *  using a thread pool to open multiple sub-threads, the main thread waits for all child threads to execute  */public static void morethread ()  {try  {int threadNum = 0;for  (int i = 0; i < 10; i++ )  {threadnum++;final int currentthreadnum = threadnum;exe.execute (New Runnable ()  {@Overridepublic  void run ()  {try {system.out.println ("sub-thread ["  +  currentthreadnum +  "] open"); Thread.Sleep (1000*10);}  catch  (interruptedexception e)  {e.printstacktrace ();} Finally{system.out.println ("sub-thread ["  + currentThreadNum + ] "end");}});   }system.out.println ("All sub-threads have been turned on"); Exe.shutdown (); System.out.println ("Shutdown (): Initiates a sequential shutdown, performs a previously submitted task, but does not accept new tasks. "); while (true) {if (exe.isterminated ()) {System.out.println (" All child threads are over! ") "); break;} Thread.Sleep (;  }} catch ) (interruptedexception e)  {e.printstacktrace (); Finally{system.out.println ("Main thread End");}}
      Child thread [1] opening sub-thread [6] opening sub-thread [2] opening sub-thread [5] opening sub-thread [9] turning on all of the Cheng Zi threads [3] Opening a sub-thread [7] Opening a child thread [10] opening shutdown (): initiating a sequential shutdown, performing a previously submitted task, But do not accept new tasks. Child thread [4] opening a child thread [8] Opening a sub-thread [6] ending a child thread [3] ending a child thread [7] ending a child thread [2] ending a child thread [1] End Child thread [5] End Child thread [9] End Child thread [4] End child thread [10] End Child thread [8] End all child threads end! Main thread End


      How Java determines if all the thread pool tasks are done

      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.