Java 多线程简单写法【秒懂】

多线程

三种写法都可以,第三种代码最少

1、方式一 普通写法

public static void main(String[] args) {
    //声明线程计数器,每执行完一个线程减一,减为0后执行主线程
    //需要几个线程就写几,多些会一直等待,少些会出现比较慢的线程还没执行完就执行主线程问题
    final CountDownLatch latch = new CountDownLatch(2);

    //第一个子线程执行
    //ExecutorService Java提供的线程池方法
    ExecutorService es1 = Executors.newSingleThreadExecutor();
    es1.execute(new Runnable() {
        @Override
        public void run() {
            try {
                //暂停三秒
                Thread.sleep(3000);
            	System.out.println("线程1已执行");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //线程执行完,方法进行减1操作
            latch.countDown();
        }
    });
    es1.shutdown();


    ExecutorService es2 = Executors.newSingleThreadExecutor();
    es2.execute(new Runnable() {
        @Override
        public void run() {
            try {
                //暂停两秒
                Thread.sleep(2000);
                System.out.println("线程2已执行");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            latch.countDown();
        }
    });
    es2.shutdown();

    try {
        System.out.println("等待执行中");
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("程序执行完毕");
}

2、方式二 使用lambda简写

public static void main(String[] args) {
    //声明线程计数器,每执行完一个线程减一,减为0后执行主线程
    //需要几个线程就写几,多些会一直等待,少些会出现比较慢的线程还没执行完就执行主线程问题
    final CountDownLatch latch = new CountDownLatch(2);

    //第一个子线程执行
    //ExecutorService Java提供的线程池方法
    ExecutorService es1 = Executors.newSingleThreadExecutor();
    es1.execute(() -> {
        try {
            //暂停三秒
            Thread.sleep(3000);
        	System.out.println("线程1已执行");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //线程执行完,方法进行减1操作
        latch.countDown();
    });
    es1.shutdown();

    ExecutorService es2 = Executors.newSingleThreadExecutor();
    es2.execute(() -> {
        try {
            //暂停两秒
            Thread.sleep(2000);
            System.out.println("线程2已执行");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //线程执行完,方法进行减1操作
        latch.countDown();
    });
    es2.shutdown();

    try {
        System.out.println("等待执行中");
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("程序执行完毕");
}

3、方式三 线程池声明多个线程

线程池声明2个线程,如果es.execute() 执行超过2个时,需要等待前面的线程执行完再执行

public static void main(String[] args) {
    //声明线程计数器,每执行完一个线程减一,减为0后执行主线程
    //需要几个线程就写几,多些会一直等待,少些会出现比较慢的线程还没执行完就执行主线程问题
    final CountDownLatch latch = new CountDownLatch(2);

    //第一个子线程执行
    //ExecutorService Java提供的线程池方法
    ExecutorService es = Executors.newFixedThreadPool(2);
    es.execute(() -> {
        try {
            //暂停三秒
            Thread.sleep(3000);
        	System.out.println("线程1已执行");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //线程执行完,方法进行减1操作
        latch.countDown();
    });

    es.execute(() -> {
        try {
            //暂停两秒
            Thread.sleep(2000);
            System.out.println("线程2已执行");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //线程执行完,方法进行减1操作
        latch.countDown();
    });

    try {
        System.out.println("等待执行中");
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        //关闭线程池
        es.shutdown();
    }

    System.out.println("程序执行完毕");
}

版权声明:本文为y393016244原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。