JAVA模拟高并发

import java.util.concurrent.CountDownLatch;

/**
 * 模拟高并发
 * User: Scott
 * Date: 2018/4/14
 * Time: 20:41
 */
public class UserDo implements Runnable {
    // 并发的用户数(同时并发的线程数)
    private static final int threadNum = 1;
    // 倒计数,用于制造线程的并发执行
    private static CountDownLatch countDownLatch = new CountDownLatch(threadNum);

    private String msg;
    public UserDo(String msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(msg);
    }

    public static void main(String[] args) {
        for(int i=0; i< threadNum; i++) {
            new Thread(new UserDo("do something " + i)).start();
            countDownLatch.countDown();
        }
    }
}

你可能感兴趣的:(JAVA模拟高并发)