diff --git a/job.go b/job.go index 88f3824..cb33e8b 100644 --- a/job.go +++ b/job.go @@ -167,15 +167,13 @@ func (j *Job) limit() bool { return false } - // 更精确的控制是加锁 - // 两次运行时间极为接近的任务才可能出现控制不精确的情况 - count := atomic.LoadInt64(j.Count) - if j.Parallels <= count { + count := atomic.AddInt64(j.Count, 1) + if j.Parallels < count { + atomic.AddInt64(j.Count, -1) j.Fail(time.Now(), fmt.Sprintf("job[%s] running on[%s] running:[%d]", j.Key(), j.runOn, count)) return true } - atomic.AddInt64(j.Count, 1) return false }