我想让线程池Executor中执行10个线程,线程结束后再执行其他语句,我该怎么设置让10个线程结束后再运行其他语句,而不是执行线程中就运行下面的语句?

解决方案 »

  1.   

    线程类有一个方法是public final boolean isAlive();你可以试一下
      

  2.   

    public class Test extends Thread  { int i = 0;

    public void run () {
    System.out.println(getName()+"  a");
    }

    public void mainrun(){
    System.out.println(getName()+"zuihou"+i++);
    }
    public static void main(String[] args) throws InterruptedException{
    Test t = new Test();
    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = new Test();
    Test t4 = new Test();
    Test t5 = new Test();
    Test t6 = new Test();
    Test t7 = new Test();
    Test t8 = new Test();
    Test t9 = new Test();
    Test t10 = new Test();
    t1.start();
    System.out.println(t1.getName());
    t2.start();
    System.out.println(t2.getName());
    t3.start();
    System.out.println(t3.getName());
    t4.start();
    System.out.println(t4.getName());
    t5.start();
    System.out.println(t5.getName());
    t6.start();
    System.out.println(t6.getName());
    t7.start();
    System.out.println(t7.getName());
    t8.start();
    System.out.println(t8.getName());
    t9.start();
    System.out.println(t9.getName());
    t10.start();
    System.out.println(t10.getName());
    System.out.println("ddd");
    t.start();
    t.join();//线程让步
    t.mainrun(); }
    }
      

  3.   

    三楼的意思我不是很明白,感觉没能解决我的问题,isAlive()方法感觉会陷入我提到的问题中,即如果用if语句判别isAlive()再决定是否进行下面的语句,那么在线程尚未运行完时,main()就会判别,以至永远不会运行下面的语句。1楼的想法也会有这个问题。
    我的意思是既然有个join()可以等待线程结束,那么有没有管理多线程的类的方法中可以等待所有线程运行完?
      

  4.   

     Excecutor shutdown 和 isShutdown  还有 isTerminated不行么,,在不行就向2说的,,用数字标记记数
      

  5.   


             Thread[] threads = new Thread[10];
             for(int i=0; i<10; i++){
                 final int index = i; 
                 threads[i] = new Thread(){
                   public void run(){
                       try{
                           Thread.sleep(1000 * index);
                       }catch(InterruptedException e){                   }
                       System.out.println("Thread " + index);
                   }
                 };
                 
                 threads[i].start();
             }
             
             for(int i=0; i<10;i++){
                 try{
                     threads[i].join();
                 }catch(InterruptedException e){
                     
                 }
             }
             
             //10个线程都执行完
             System.out.println("done");用join()判断
      

  6.   


    我不是很明白调用这两方法后 t.start();t.join();打印出来的结果是:Thread-10 a
    Thread-9 a
    Thread-8 a
    Thread-7 a
    Thread-6 a
    Thread-5 a
    Thread-4 a
    Thread-3 a
    Thread-2 a
    Thread-1 a
    ddd
    Thread-0 a
    Thread-0zuihou0
    有人能说明下嘛!
      

  7.   

    你可以用信号量,每启动一个线程就给信号量加一,结束一个就减一,全部结束后信号量就空了。然后再另外一个线程里用WaitForSingleObject等待,当发现信号量为0的时候就继续……
      

  8.   

    抱歉,WaitForSingleObject是Windows编程里的,Java里用wait(),写到一半思维跳到Windows开发去了……
      

  9.   

    java里面有java.util.concurrent,用来做多线程.里面的ThreadPool可以比较方便的做这些事情
      

  10.   

     isAlive();Thread t1=new Thread("aaa");
    if(t1.isAlive)
    {
    System.out.print("yes");
    }
      

  11.   

    用循环对每个线程调用join(),即可