如何保证子线程先执行完成在再行主线程

本文介绍了在Java编程中确保子线程先于主线程执行完成的几种常见方法,包括使用join()、CountdownLatch、线程池的isDone检查、while循环判断线程状态、LockSupport工具、CyclicBarrier同步屏障以及BlockQueue和ThreadPoolTaskExecutor的使用。这些方法在多线程编程中用于协调线程间的执行顺序。

使用join()

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子正在执行");
        },"子线程");
        thread.start();
        thread.join();

        System.out.println("主线程执行" );

    }

使用CountdownLatch

        CountDownLatch c = new CountDownLatch(1);
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子正在执行");
            c.countDown();
        },"子线程");
        thread.start();
        c.await();
        System.out.println("主线程执行" );


    }

使用线程池的while (submit.isDone())

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService executorService = Executors.newFixedThreadPool(1);

        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子正在执行");
        },"子线程");

        Future<?> submit = executorService.submit(thread);
        submit.get();
        while (submit.isDone()){
            System.out.println("子线程结束");
            break;
        }
        System.out.println("主线程执行" );
        executorService.shutdown();

    }

使用while(true){if(!thread.isAlive())}

  public static void main(String[] args) throws InterruptedException, ExecutionException {


        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子正在执行");
        },"子线程");

        thread.start();
        while (true){
            if(!thread.isAlive()) {
                System.out.println("子线程结束");
                break;
            }
        }
        System.out.println("主线程执行" );

    }

LockSupport

  public static void main(String[] args) throws InterruptedException, ExecutionException {
        //main线程
        Thread thread1 = Thread.currentThread();
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子正在执行");
            //解主线程
            LockSupport.unpark(thread1);
        }, "子线程");
        thread.start();
        //阻塞主线程
        LockSupport.park();
        System.out.println("子线程执行结束");

        System.out.println("主线程执行");
    }

使用CyclicBarrier

等待所有或一组线程达到一个状态,然后全部放行。
    public static void main(String[] args) throws InterruptedException, Exception {
        CyclicBarrier c = new CyclicBarrier(2);
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子正在执行");
            try {
                c.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }, "子线程");
        thread.start();
        c.await();
        System.out.println("子线程执行结束");

        System.out.println("主线程执行");
    }

使用BlockQueue

    public static void main(String[] args) throws InterruptedException, Exception {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(10);
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            blockingQueue.add("");
            System.out.println("子正在执行");
        }, "子线程");
        thread.start();

        blockingQueue.take();
        System.out.println("子线程执行结束");

        System.out.println("主线程执行");
    }

使用ThreadPoolTaskExecutor

这种情况适用于先初始化线程池,然后执行任务。当存货线程数小于0时然后结束。最后主线程执行。
main函数不容易模拟出子线程执行完毕后主线程再执行的情况。
  public static void main(String[] args) throws InterruptedException, ExecutionException {

        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(1);
        threadPoolTaskExecutor.setMaxPoolSize(2);
        threadPoolTaskExecutor.setKeepAliveSeconds(3000);
        threadPoolTaskExecutor.setQueueCapacity(1000);
        threadPoolTaskExecutor.setThreadNamePrefix("CURRENCY-THREAD-POOL-" );
        threadPoolTaskExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                log.error("触发拒绝策略 , 当前 活跃线程数 {} , 队列长度 {} ", executor.getActiveCount(), executor.getQueue().size());
            }
        });
        threadPoolTaskExecutor.initialize();

        Thread thread = new Thread(() -> {
            for(int x = 0; x< 10000; x++){}
            System.out.println("子正在执行");
        },"子线程");

        // 此处使用了没有返回值的方法
        threadPoolTaskExecutor.execute(thread);
        while (true) {
            if (threadPoolTaskExecutor.getActiveCount() <= 0) {
                System.out.println(">>>>>>>>>>>>>>>>>>>>任务执行完毕<<<<<<<<<<<<<<<<<<<");
                break;
            }
        }
        System.out.println("主线程执行" );
        threadPoolTaskExecutor.shutdown();

    }
总结:只要保证主线阻塞,子线程唤醒。就可以达到子线程执行完毕,主线程再执行的效果
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心有城府,腹有良谋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值