标签:
目的:程序执行完任务后,再执行其他的任务。 
实现原理: 
使用Thread类的join()方法时。当一个线程对象的join()方法被调用是,调用它的线程将被挂起,直到这个线程对象完成它的任务。 
代码:引用的java7并发编程实战手册示例代码 
package com.packtpub.java7.concurrency.chapter1.recipe6.task;
import java.util.Date; 
import java.util.concurrent.TimeUnit;
/** 
 * Class that simulates an initialization operation. It sleeps during four seconds 
 * 
 */ 
public class DataSourcesLoader implements Runnable {
/**
 * Main method of the class
 */
@Override
public void run() {
    // Writes a messsage
    System.out.printf("Begining data sources loading: %s\n",new Date());
    // Sleeps four seconds
    try {
        TimeUnit.SECONDS.sleep(4);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // Writes a message
    System.out.printf("Data sources loading has finished: %s\n",new Date());
}
}
package com.packtpub.java7.concurrency.chapter1.recipe6.task;
import java.util.Date; 
import java.util.concurrent.TimeUnit;
/** 
 * Class that simulates an initialization operation. It sleeps during six seconds  
 * 
 */ 
public class NetworkConnectionsLoader implements Runnable {
/**
 * Main method of the class
 */
@Override
public void run() {
    // Writes a message
    System.out.printf("Begining network connections loading: %s\n",new Date());
    // Sleep six seconds
    try {
        TimeUnit.SECONDS.sleep(6);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // Writes a message
    System.out.printf("Network connections loading has finished: %s\n",new Date());
}
}
package com.packtpub.java7.concurrency.chapter1.recipe6.core;
import java.util.Date;
import com.packtpub.java7.concurrency.chapter1.recipe6.task.DataSourcesLoader; 
import com.packtpub.java7.concurrency.chapter1.recipe6.task.NetworkConnectionsLoader;
/** 
 * Main class of the Example. Create and start two initialization tasks 
 * and wait for their finish 
 * 
 */ 
public class Main {
/**
 * Main method of the class. Create and star two initialization tasks
 * and wait for their finish
 * @param args
 */
public static void main(String[] args) {
    // Creates and starts a DataSourceLoader runnable object
    DataSourcesLoader dsLoader = new DataSourcesLoader();
    Thread thread1 = new Thread(dsLoader,"DataSourceThread");
    thread1.start();
    // Creates and starts a NetworkConnectionsLoader runnable object
    NetworkConnectionsLoader ncLoader = new NetworkConnectionsLoader();
    Thread thread2 = new Thread(ncLoader,"NetworkConnectionLoader");
    thread2.start();
    // Wait for the finalization of the two threads
    try {
        thread1.join();
        thread2.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // Waits a message
    System.out.printf("Main: Configuration has been loaded: %s\n",new Date());
}
}
扩展: 
 
通过查看源码看到join的重载方法 
join(): 
join(long,int) 
本质调用的都是join(long) 
 
 
 
通过查看反编译后的源代码,可知道只要线程运行结束(isAlive)或者时间到了之后(delay<=0;break;),被挂起的线程将再次可以执行。
标签:
原文地址:http://blog.csdn.net/u013323412/article/details/51364482