• Java7并发编程实战(一) 线程的等待


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

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

      

    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
    ",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
    ",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
    ",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
    ",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
    ",new Date());
        }
    }
  • 相关阅读:
    实验室机器OracleXE客户端连接远程Oracle服务器
    PLSQL Developer 远程连接Oracle XE数据库(其他Oracle数据库类似)
    VB进制转化大全
    给定一个硬盘标识,umout掉所有相关挂载
    linux读取硬盘指定扇区
    开发板上挂载NTFS分区
    write error:file too large!!!
    一些自用的c函数
    主引导记录(MBR)分析
    strtok_r函数学习
  • 原文地址:https://www.cnblogs.com/LIANQQ/p/4633832.html
Copyright © 2020-2023  润新知