1. 程式人生 > >【學習筆記】Java-Concurrent-多執行緒測試模板

【學習筆記】Java-Concurrent-多執行緒測試模板

import java.util.concurrent.CountDownLatch;

/**
 * 多執行緒測試模板
 *
 * @author Mairuis
 * @date 2018/10/11
 */
public class ConcurrentTest {

    public static class Context{
        public Context(){

        }
    }
    public static CountDownLatch countDownLatch = new CountDownLatch(3);
    public static Context context = new Context();
    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(ConcurrentTest::firstThread);
        Thread t2 = new Thread(ConcurrentTest::secondThread);
        t1.start();
        t2.start();
        countDownLatch.countDown();
        singleThread();
    }

    public static void firstThread(){
        try {
            countDownLatch.countDown();
            countDownLatch.await();
            firstThread0(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void secondThread(){
        try {
            countDownLatch.countDown();
            countDownLatch.await();
            secondThread0(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 最後一個執行的執行緒
     * @throws Exception
     */
    public static void singleThread() throws  Exception{

    }

    /**
     * 第一個執行緒 與第二個同時執行
     * @param context
     * @throws Exception
     */
    public static void firstThread0(Context context) throws Exception{

    }

    /**
     * 第二個執行緒
     * @param context
     * @throws Exception
     */
    public static void secondThread0(Context context) throws Exception{

    }
}