博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
模拟并发请求
阅读量:7191 次
发布时间:2019-06-29

本文共 1608 字,大约阅读时间需要 5 分钟。

hot3.png

package com.glass.fileupload.app.utils.thread.upload;

import java.util.concurrent.CountDownLatch;

public class LatchTest {

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

        Runnable taskTemp = new Runnable() {

// 注意,此处是非线程安全的,留坑

            private int iCounter;

           

            public void run() {
                for(int i = 0; i < 10; i++) {
                    // 发起请求
//                    HttpClientOp.doGet("https://www.baidu.com/");
                   
                    iCounter++;
                    System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        LatchTest latchTest = new LatchTest();

        latchTest.startTaskAllInOnce(5, taskTemp);
    }

    public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {

        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(threadNums);
        for(int i = 0; i < threadNums; i++) {
            Thread t = new Thread() {
                public void run() {
                    try {
                        // 使线程在此等待,当开始门打开时,一起涌入门中
                        startGate.await();
                        try {
                            task.run();
                        } finally {
                            // 将结束门减1,减到0时,就可以开启结束门了
                            endGate.countDown();
                        }
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                }
            };
            t.start();
        }
        long startTime = System.nanoTime();
        System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
        // 因开启门只需一个开关,所以立马就开启开始门
        startGate.countDown();
        // 等等结束门开启
        endGate.await();
        long endTime = System.nanoTime();
        System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
        return endTime - startTime;
    }
}

转载于:https://my.oschina.net/thomas2/blog/2994988

你可能感兴趣的文章
[转] 理解NLP中的卷积&&Pooling
查看>>
【BZOJ3670】【NOI2014】动物园 [KMP][倍增]
查看>>
念数字(15)
查看>>
online_judge_1474
查看>>
python-*args与**kwagrs 动态形参使用说明
查看>>
CSS列表逆序
查看>>
javascript方法--bind()
查看>>
spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
查看>>
Install fail! Error: [@@babel/runtime/core-js/object/keys]
查看>>
基于vue 的 UI框架 -- Mint UI
查看>>
redux-saga 异步流
查看>>
Mina入门实例(一)
查看>>
HTML基本标签
查看>>
AngularJs学习——实现数据绑定的三种方式
查看>>
Ubuntu 安装yii2 advanced版 遇到的坑
查看>>
UVA - 11400 Lighting System Design
查看>>
[HNOI2005]狡猾的商人
查看>>
剑指offer(二十三,二十四,二十五)最小的k个数,连续子数组的最大和,链表中环的入口节点...
查看>>
linux下面如何让一个软件/命令开机自启动
查看>>
P4306 [JSOI2010]连通数
查看>>