你可以用线程的join()方法,加入主线程

解决方案 »

  1.   

    还可以使用回调方法:
    public interface Servlet{
        void callback();
    }
    public class ServerImpl implements Server{
        public void callback(){
            System.out.println("线程已经执行完毕");
        }
        public void static main(String[]){
            Server server=new ServerImpl();
            ClientThread client=new ClientThread(server);
            Thread testClient=new Thread(client);
            testClient.start();
        }
    }
    public class ClientThread implements Runnable{
        private Server server;
        public ClientThread(Server server){
            this.server=server;
        }
        public void run(){
           for(int i=0;i<1000;i++){       }
           server.callback();
        }
    }
      

  2.   

    notify()方法具体如何使用啊
    能不能说得稍微详细一点.
      

  3.   

    是这样,你的多个线程如果没有调用wait(),则他们都是并行执行的,也就不用“告之”
    主程序;Java包含了通过wait( ),notify( )和notifyAll( )方法实现的一个进程间通信机制。wait( ) 告知被调用的线程放弃管程进入睡眠直到其他线程进入相同管程并且调用notify( )。notify( ) 恢复相同对象中第一个调用wait( ) 的线程。
      

  4.   

    我总共new 了三个线程.
    它们并行执行,每个线程都执行一个"批量"任务.
    我希望在这个线程都执行完之后能够"告之"主程序,
    任务已经全部完成.否则我无法知道是否任务是否已经完成.
      

  5.   

    执行线程的时候start()以后主程序wait(),线程执行完以后notify();
      

  6.   

    主程序MyThread m = new MyThread();
    m.start();wait(); // 这样?
    在wait()之后的语句,肯定是在线程结束之后
    才执行的吗?线程代码
    在run函数中添加 ...
    public void run() {
       ...
       notify(); // 最后一行
    }是这样吗?
      

  7.   

    回复人: shangqiao(伤桥) 的方法比较简单哈~
      

  8.   

    按照shangqiao的方法
    它是直接去调用主程序的方法
    如果有多个线程的话,那又如何处理呢