【问题标题】:How to call method multiple times in the same time如何同时多次调用方法
【发布时间】:2020-09-25 15:16:27
【问题描述】:

我需要编写一些测试来同时多次调用我的方法,是否可以使用 MvcMock 之类的?

我想获得与在 bash 中使用 curl & curl & curl 所获得的结果相同的结果(异步运行多个 curl 请求)——我的方法抛出乐观锁定异常,因为每个请求都修改同一个实体,但我的所有测试都是绿色的。

我知道解决方案(用 PESSIMISTIC 锁定)并且效果很好,但我不知道如何测试它。

【问题讨论】:

    标签: java spring multithreading hibernate junit


    【解决方案1】:

    有什么东西会阻碍使用基本的 Java 多线程吗? 类似的东西

        ExecutorService EXEC = Executors.newCachedThreadPool();
        List<Callable<String>> tasks = new ArrayList<>();
        Callable<String> c = () -> {
            System.out.println("Hello");
            return "";
        };
        tasks.add(c);
        tasks.add(c);
        tasks.add(c);
        List<Future<String>> results = EXEC.invokeAll(tasks);
    

    (将 Sys 替换为您的方法调用)

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      相关资源
      最近更新 更多