package org.wcs.utils;

import java.util.List;
import java.util.concurrent.CountDownLatch;

public class AppThreadUtils {

    /**
     * 线程休眠
     *
     * @param millis 毫秒数
     */
    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException ignored) {
        }
    }

    /**
     * 启动新线程
     *
     * @param runnable 线程任务
     */
    public static void startNewThread(Runnable runnable) {
        new Thread(runnable).start();
    }

    /**
     * 等待所有任务完成
     *
     * @param runnables 任务列表
     */
    public static void waitAllTaskComplete(List<Runnable> runnables) {
        CountDownLatch countDownLatch = new CountDownLatch(runnables.size());
        for (Runnable runnable : runnables) {
            new Thread(() -> {
                try {
                    runnable.run();
                } catch (Exception ignored) {
                } finally {
                    countDownLatch.countDown();
                }
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException ignored) {
        }
    }

}
