使用Java程序来等待线程结束_极悦注册
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 使用Java程序来等待线程结束

使用Java程序来等待线程结束

更新时间:2022-11-16 09:50:01 来源:极悦 浏览1235次

此示例显示了一个 Java™ 程序启动多个线程,等待它们完成,并在完成后检查它们的状态。

/*
FileName: ATEST15.java 
The output of this example is as follows:
 Entered the testcase
 Create some worker threads
 Start the thread
 Thread Thread-1: Entered
 Thread Thread-1: Working
 Thread Thread-2: Entered
 Thread Thread-2: Working
 Thread Thread-3: Entered
 Thread Thread-3: Working
 Thread Thread-4: Entered
 Thread Thread-4: Working
 Wait for worker threads to complete
 Thread Thread-5: Entered
 Thread Thread-5: Working
 Thread Thread-1: Done with work
 Thread Thread-2: Done with work
 Thread Thread-3: Done with work
 Thread Thread-4: Done with work
 Thread Thread-5: Done with work
 Check all thread's results
 Testcase completed
*/
import java.lang.*; 
public class ATEST15 {
   public final static int THREADGROUPSIZE   = 5; 
   static class theThread extends Thread {
      public final static int THREADPASS     = 0;
      public final static int THREADFAIL     = 1;
      int         _status; 
      public int status() {
         return _status;
      }
      public theThread() {
         _status = THREADFAIL;
      }
      public void run() {
         System.out.print("Thread " + getName() + ": Entered\n");
         System.out.print("Thread " + getName() + ": Working\n");
         safeSleep(15000, "Thread " + getName() + " work");
         System.out.print("Thread " + getName() + ": Done with work\n");
         _status = THREADPASS;
      }
   } 
   public static void main(String argv[]) {
      int           i=0;
      theThread     thread[] = new theThread[THREADGROUPSIZE]; 
      System.out.print("Entered the testcase\n"); 
      System.out.print("Create some worker threads\n");
      System.out.print("Start the thread\n");
      /* We won't use a ThreadGroup for this example, because we'd */
      /* still have to join all the threads individually           */
      for (i=0; i <THREADGROUPSIZE; ++i) {
  thread[i] = new theThread();
         thread[i].start();
      } 
      System.out.print("Wait for worker threads to complete\n");
      for (i=0; i <THREADGROUPSIZE; ++i) {
try {
            thread[i].join();
         }
         catch (InterruptedException e) {
            System.out.print("Join interrupted\n");
         }
      }
      System.out.print("Check all thread's results\n");
      for (i=0; i <THREADGROUPSIZE; ++1) {
if (thread[i].status() != theThread.THREADPASS) {
            System.out.print("Unexpected thread status\n");
         }
      } 
      System.out.print("Testcase completed\n");
      System.exit(0);
   } 
   public static void safeSleep(long milliseconds, String s) {
      try {
         Thread.sleep(milliseconds);
      }
      catch (InterruptedException e) {
         System.out.print(s);
      }
   } 
}          

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>