From f0a958e0bed8aafec49e660e23dcbd23c25d63a2 Mon Sep 17 00:00:00 2001 From: dsyer Date: Sat, 15 Aug 2009 09:19:16 +0000 Subject: [PATCH] RESOLVED - issue BATCH-1362: Threads spinning doing nothing at end of multi-threaded Step Extra check for deadlock hopefully will fix build issues. --- .../support/TaskExecutorRepeatTemplate.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplate.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplate.java index 2739dfa99..411865fa5 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplate.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplate.java @@ -402,7 +402,6 @@ public class TaskExecutorRepeatTemplate extends RepeatTemplate { synchronized (lock) { - active--; stillActive = active > 0 || paused; @@ -417,12 +416,23 @@ public class TaskExecutorRepeatTemplate extends RepeatTemplate { if (stillActive) { logger.debug("Waiting for other active callbacks to finish."); synchronized (lock) { - try { - lock.wait(); - } - catch (InterruptedException e) { - logger.info("Interrupted waiting for active callbacks"); - Thread.currentThread().interrupt(); + while (stillActive) { + try { + /* + * Need a timed wait here to avoid deadlock in + * the case that stillActive changed its value + * between where it was computed and this wait. + * In that case another thread might have + * already sent the notify() message and then we + * would miss it here. + */ + lock.wait(2000L); + stillActive = active > 0 || paused; + } + catch (InterruptedException e) { + logger.info("Interrupted waiting for active callbacks"); + Thread.currentThread().interrupt(); + } } } } @@ -441,8 +451,9 @@ public class TaskExecutorRepeatTemplate extends RepeatTemplate { paused = false; lock.notifyAll(); } - } else { - synchronized (lock) { + } + else { + synchronized (lock) { paused = true; } }