* 和 ? 之间的区别在 Spring @Scheduled(cron=".....")

Difference between * and ? in Spring @Scheduled(cron=".....")

我一直在查看 Spring 调度任务的引导示例 (https://spring.io/guides/gs/scheduling-tasks/) and reading through some documentation (https://javahunter.wordpress.com/2011/05/05/cronscheduler-in-spring/),我看到 * 和 ?几乎可以互换使用。

例如,行

@Scheduled(cron = "0 15 10 ? * *")

@Scheduled(cron = "0 15 10 * * ?")

做同样的事情。那么 * 和 ?

有什么区别呢?

asterix 代表所有可能的值。问号应该用于非特定值

*("all values") - used to select all values within a field. For example, "" in the minute field means *"every minute".

? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification.

复制自 tutorial

该教程已过时。符号 ? 的意思 与符号 *.

完全相同

从 Spring 版本 3.1.2.RELEASE 开始,调用层次结构如下:

  1. 构造函数CronTrigger(String)调用构造函数CronSequenceGenerator(String)
  2. CronSequenceGenerator(String) 呼叫 parse(String)
  3. parse(String) 呼叫 setDays(BitSet bits, String field, int max).

其实现很明确:

private void setDays(BitSet bits, String field, int max) {
    if (field.contains("?")) {
        field = "*";
    }
    setNumberHits(bits, field, 0, max);
}

因此,如果 ?,则 *