scheduleAtFixedRate的并发ScheduledThreadPoolExecutor的IllegalArgumentException

时间:2017-07-14 00:02:43

标签: java jvm-hotspot

我在Mac上运行HotSpot,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

我在frequency文件中设置Runner.java时出错。例如,如果我在下面的任务中设置25,5替换50,我将得到错误。那是为什么?

int frequency = 50 * runSpecification.objectSize / runSpecification.allocationRatePerSecond; // 25 will fail and yield bug

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
.
└── main
    └── java
        └── eu
            └── plumbr
                └── gc
                    ├── Consumer.java
                    ├── Producer.java
                    ├── RunSpecification.java
                    └── Runner.java

5 directories, 4 files

Consumer.java

package eu.plumbr.gc;

import java.util.Deque;

public class Consumer implements Runnable{

  private Deque<byte[]> deque;

  public Consumer(Deque<byte[]> deque) {
    this.deque = deque;
  }

  @Override
  public void run() {
    deque.poll();
  }
}

Producer.java

package eu.plumbr.gc;

import java.util.Deque;

public class Producer implements Runnable {

  private Deque<byte[]> deque;
  private int objectSize;

  public Producer(Deque<byte[]> deque, int objectSize) {
    this.deque = deque;
    this.objectSize = objectSize;
  }

  @Override
  public void run() {
    deque.add(new byte[objectSize]);
  }
}

Runner.java

package eu.plumbr.gc;

import java.util.ArrayDeque;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Runner {

  private RunSpecification runSpecification;
  private ScheduledExecutorService executorService;

  public Runner(RunSpecification runSpecification, ScheduledExecutorService executorService) {
    this.runSpecification = runSpecification;
    this.executorService = executorService;
  }


  public static void main(String[] args) throws InterruptedException {
    Runner runner = new Runner(new RunSpecification(10 * 1024, 1024 * 500, 5), Executors.newScheduledThreadPool(2));
    runner.run();
  }

  public void run() {
    ArrayDeque<byte[]> deque = new ArrayDeque<byte[]>();
    Producer producer = new Producer(deque, runSpecification.objectSize);
    Consumer consumer = new Consumer(deque);

    int frequency = 50 * runSpecification.objectSize / runSpecification.allocationRatePerSecond; // 25 will fail and yield bug
    executorService.scheduleAtFixedRate(producer, 0, frequency, TimeUnit.MILLISECONDS);
    executorService.scheduleAtFixedRate(consumer, 1000 * runSpecification.ttl, frequency, TimeUnit.MILLISECONDS);
  }
}

RunSpecification.java

package eu.plumbr.gc;

public class RunSpecification {
  public int objectSize;
  public int allocationRatePerSecond;
  public int ttl;

  public RunSpecification(int objectSize, int allocationRatePerSecond, int ttl) {
    this.objectSize = objectSize;
    this.allocationRatePerSecond = allocationRatePerSecond;
    this.ttl = ttl;
  }
}

参考:

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

2 个答案:

答案 0 :(得分:2)

如果您阅读了javadoc,您将看到

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

不知道您的确切输入,但我会说

int frequency = 50 * runSpecification.objectSize / 
                      runSpecification.allocationRatePerSecond;

frequency大于0,但使用25则不是

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

进行验证
    int objectSize = 10240; 
    int allocationRatePerSecond = 512000;

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

<强>输出

1
0

答案 1 :(得分:0)

基于'Scary Wombat'建议的解决方案,我进行了更改并摆脱了错误。即 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并成功!!