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();
        }
    }
}

阅读更多
上一篇SQL统计每年在校学生总人数
下一篇数据库事务的四大特性以及事务的隔离级别
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭