United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6725789 ScheduledExecutorService does not work as expected in jdk7/6/5
JDK-6725789 : ScheduledExecutorService does not work as expected in jdk7/6/5

Details
Type:
Bug
Submit Date:
2008-07-15
Status:
Closed
Updated Date:
2010-12-11
Project Name:
JDK
Resolved Date:
2010-12-11
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.util.concurrent
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:
Backport:
Backport:
Relates:

Sub Tasks

Description
ScheduledExecutorService does not work as we expect.

The test programs schedules the execution of MyTask().
--
When Mytask() is scheduled  in scheduleWithFixedDelay() as follows,
 -initialDelay is 0 : Execute right now
 -delay is 1000[msec] : Execute every 1000[msec]
the MyTask() sometimes is not executed.
Initial execution of MyTask() is not executed and scheduled MyTask() is not executed also.

CONFIGURATION:
OS : WindowsXP(SP2,Japanese)
JRE/JDK : jdk7b30/jdk6u7/jdk5u16

Note:
 The reporter says, this occus in RHEL(x86) and submitter confirms this problem occurs in Solaris10.

FREQUENCY:
 The problem can be reproducible so often. 
The test program sometimes finishes correctly.

EXPECTED:
 The followings message will shows up.

ACTUAL:
 No message. The execution seems hang.

                                    

Comments
WORK AROUND

Avoid using delays far in the future.
                                     
2008-07-16
SUGGESTED FIX

if (triggerTime < 0)
  triggerTime = Long.MAX_VALUE;
                                     
2008-07-17
EVALUATION

As the submitter had determined the various "schedule" methods contain code of the form:

long triggerTime = now() + delay;

and if the delay is sufficiently large then overflow will occur.

When overflow occurs a negative delay-time can be stored into the delay-queue and this causes the delay queue to operate incorrectly. If you place a correct entry in the queue and then add a negative entry, while the correct entry is still there, then the correct entry is pushed down and no task ever becomes scheduled to run - and we have the hang as reported.

However, if the correct task is removed from the queue by a worker thread, prior to the negative task being submitted, then the task is correctly replaced in the queue and the test passes.

It was curious to find that:

java Test

passes, while

java -showversion Test

always hangs.
                                     
2008-07-17
EVALUATION

There is actually a more subtle underlying issue with the data structure that hides a number of potential bugs. Martin Buchholz has prepared a complete fix for delivery in to OpenJDK.
                                     
2008-07-28
EVALUATION

http://hg.openjdk.java.net/jdk7/tl/jdk/rev/f33c3846cecb
                                     
2008-08-01



Hardware and Software, Engineered to Work Together