Java scheduleAtFixedRate的并发ScheduledThreadPoolExecutor的IllegalArgumentException

Java scheduleAtFixedRate的并发ScheduledThreadPoolExecutor的IllegalArgumentException,java,jvm-hotspot,Java,Jvm Hotspot,我正在Mac上运行热点JDK8的测试文件。我使用IntelliJ IDEA来运行这个java程序 IntelliJ IDEA 2017.1.2 Build #IC-171.4249.39, built on April 25, 2017 JRE: 1.8.0_112-release-736-b16 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.12.5 我在Runner.java文件中设置频率时出错。例

我正在Mac上运行热点JDK8的测试文件。我使用IntelliJ IDEA来运行这个java程序

IntelliJ IDEA 2017.1.2
Build #IC-171.4249.39, built on April 25, 2017
JRE: 1.8.0_112-release-736-b16 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.12.5

我在
Runner.java
文件中设置
频率时出错。例如,如果我在下面的作业中将25,5设置为50,我将得到错误。为什么呢

int frequency=50*runSpecification.objectSize/runSpecification.allocationRatePerSecond;//25将失败并产生错误

Exception in thread "main" java.lang.IllegalArgumentException
    at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:565)
    at eu.plumbr.gc.Runner.run(Runner.java:30)
    at eu.plumbr.gc.Runner.main(Runner.java:21)

Process finished with exit code 1

Consumer.java 参考:


如果您阅读Java文档,您将看到

IllegalArgumentException-如果周期小于或等于零

我不知道你的确切意见,但我想说的是

int frequency = 50 * runSpecification.objectSize / 
                      runSpecification.allocationRatePerSecond;
频率
大于0,但使用25则不大于0

请记住,您正在进行整数除法,这可以通过

    int objectSize = 10240; 
    int allocationRatePerSecond = 512000;

    System.out.println(50 * objectSize / allocationRatePerSecond);
    System.out.println(25 * objectSize / allocationRatePerSecond);
输出

1
0

如果您阅读javadocs,您将看到

IllegalArgumentException-如果周期小于或等于零

我不知道你的确切意见,但我想说的是

int frequency = 50 * runSpecification.objectSize / 
                      runSpecification.allocationRatePerSecond;
频率
大于0,但使用25则不大于0

请记住,您正在进行整数除法,这可以通过

    int objectSize = 10240; 
    int allocationRatePerSecond = 512000;

    System.out.println(50 * objectSize / allocationRatePerSecond);
    System.out.println(25 * objectSize / allocationRatePerSecond);
输出

1
0

基于“可怕的袋熊”提出的解决方案,我做了更改并消除了错误。即,IllegalArgumentException-如果周期小于或等于零

初始代码:

private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this, 0, 1, TimeUnit.valueOf(DAYS));
更正代码:

private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.valueOf(DAYS));

将第二个参数从0更改为1,它成功了

基于“可怕的袋熊”提出的解决方案,我已经做了更改并消除了错误。即,IllegalArgumentException-如果周期小于或等于零

初始代码:

private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this, 0, 1, TimeUnit.valueOf(DAYS));
更正代码:

private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.valueOf(DAYS));

将第二个参数从0更改为1,它成功了

这是大量的代码这是大量的代码RunSpecification.objectSize=10240;runSpecification.allocationRatePerSecond=512000;runSpecification.objectSize=10240;runSpecification.allocationRatePerSecond=512000;