博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java7并发编程实战(一) 线程的等待
阅读量:4323 次
发布时间:2019-06-06

本文共 2377 字,大约阅读时间需要 7 分钟。

试想一个情景,有两个线程同时工作,还有主线程,一个线程负责初始化网络,一个线程负责初始化资源,然后需要两个线程都执行完毕后,才能执行主线程

  首先创建一个初始化资源的线程

  

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());    }}
View Code

  

  然后创建一个初始化网络的线程

  

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());    }}
View Code

  

  通过TimeUnit.SECONDS.sleep()方法; 进行休眠,

 

  然后主线程执行,通过join方法,当一个线程对象的join方法被调用时,调用他的线程将会被挂起,知道这个线程来完成这些初始化任务,我们在主线程分别调用两个Thread的join方法,那么主线程会等到两个线程都执行完毕才会执行下去。

  

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());    }}

 

转载于:https://www.cnblogs.com/LIANQQ/p/4633832.html

你可能感兴趣的文章
linux安全设置
查看>>
Myflight航班查询系统
查看>>
团队-团队编程项目爬取豆瓣电影top250-代码设计规范
查看>>
【linux驱动分析】之dm9000驱动分析(六):dm9000_init和dm9000_probe的实现
查看>>
json具体解释
查看>>
十一:Java之GUI图形Awt和Swing
查看>>
.net在arraylist用法
查看>>
android程序报错“error launching activity com.android.ddmlib.shellcommandunresponsiveexception”的解决方式...
查看>>
ORACLE中CONSTRAINT的四对属性
查看>>
DbVisualizer Pro 9.5.2中文乱码问题
查看>>
numpy.tile()
查看>>
[bzoj3944] Sum
查看>>
hadoop2.7节点的动态增加与删除
查看>>
Ogre: 天空
查看>>
关于NSDictionary的一点感悟
查看>>
CSS长度单位:px和pt的区别
查看>>
50.分治算法练习: 二分算法: 2703 奶牛代理商 XII
查看>>
Wampserver 403问题
查看>>
mysql日志详细解析
查看>>
解决关闭app权限弹框后无法识别页面对象问题
查看>>