Thread.join()(如果其他线程只是发送消息,没有其他任务)
或者使用一个信号量
主线程s.wait();
其他线程socket消息发完后s.notify();

解决方案 »

  1.   

    // Create and start a thread
        Thread thread = new MyThread();
        thread.start();
        
        // Check if the thread has finished in a non-blocking way
        if (thread.isAlive()) {
            // Thread has not finished
        } else {
            // Finished
        }
        
        // Wait for the thread to finish but don't wait longer than a
        // specified time
        long delayMillis = 5000; // 5 seconds
        try {
            thread.join(delayMillis);
        
            if (thread.isAlive()) {
                // Timeout occurred; thread has not finished
            } else {
                // Finished
            }
        } catch (InterruptedException e) {
            // Thread was interrupted
        }
        
        // Wait indefinitely for the thread to finish
        try {
            thread.join();
            // Finished
        } catch (InterruptedException e) {
            // Thread was interrupted
        }
      

  2.   

    在主线程里用Thread.sleep()不行吗?