Skip to content

Commit fa2cc69

Browse files
committed
Improve ScheduleWithTimeout
1 parent e36bd01 commit fa2cc69

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

worker/pool.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,8 @@ func (workerPoolSelf *DefaultWorkerPool) ScheduleWithTimeout(fn func(), timeout
329329
}
330330

331331
deadline := time.Now().Add(timeout)
332-
workerPoolSelf.lock.Lock()
333-
workerPoolSelf.scheduleWaitCount++
334-
workerPoolSelf.lock.Unlock()
335-
go func() {
336-
workerPoolSelf.lock.Lock()
337-
workerPoolSelf.scheduleWaitCount--
338-
workerPoolSelf.lock.Unlock()
339-
}()
332+
workerPoolSelf.addScheduleWaitCount(1)
333+
go workerPoolSelf.addScheduleWaitCount(-1)
340334

341335
for {
342336
if workerPoolSelf.IsClosed() {
@@ -358,6 +352,12 @@ func (workerPoolSelf *DefaultWorkerPool) ScheduleWithTimeout(fn func(), timeout
358352
return err
359353
}
360354

355+
func (workerPoolSelf *DefaultWorkerPool) addScheduleWaitCount(amount int) {
356+
workerPoolSelf.lock.Lock()
357+
workerPoolSelf.scheduleWaitCount += amount
358+
workerPoolSelf.lock.Unlock()
359+
}
360+
361361
// Invokable
362362

363363
// Invokable Invokable inspired by Java ExecutorService

0 commit comments

Comments
 (0)