This discussion is archived
2 Replies Latest reply: Dec 23, 2011 4:44 AM by gimbal2 RSS

Inconsistent Behavior of Java Threads...Urgent Help Needed

907213 Newbie
Currently Being Moderated
I have one Scheduler thread which creates 3 other threads (let’s say 3 modules) and make them sleep for some time let’s say for 10 mins...

I want to run Module1 to run every 5 mins, Module 2 to run every 10 mins and module 3 to run every 15 mins.

My requirement is that Every 5 mins, my main scheduler thread should check what all modules are eligible to be run at that point of time.

i.e. at 12:00 hours it should find all 3 to be eligible
at 12:05 it should find only Module 1 to be eligible
at 12:10 it should find Module 1 and 2 to be eligible
at 12:15 it should find module 1 and 3 to be eligible to be run etc.

So based on these calculation, it should wake up the required thread of the required module(which are in sleep state) and then run them and then make them sleep again.

In my current code, the scheduler is calculating the eligible threads properly and puts them in an array e.g. at 12.15 the array looks like Array [Module1, Module3], once it calculates, it wakes up all the 3 threads and then first compares Module 1 (first of the 3 waken up threads ) and compares with all the first modules in the array. Once it matches Module1 to Module 1, it runs the module 1 thread and calls the run method of Module1 and once over it makes module 1 thread to sleep.

Then in parallel it compares the waken up Module 2 with the first module inside the list. And as module 2 does not find any match for itself inside the array, it goes for sleep.

Again in parallel, it compared the waken up module 3 with the modules in the list. And module 3 does not match the first element i.e. Module 1. Hence even module 3 goes for sleep.

Now when module 1 has been completed, my scheduler removes module 1 from the Array.

Now the array consists of the left out “Module 3”.

Again the scheduler wakes up all the 3 threads and does the same comparison again. So only module 3 matches and the other 2 go to sleep. Once module 3 is executed the same also goes to sleep.

So ultimately, my Scheduler is able to run the scheduled modules at a particular point of time.


The problem is : Maximum times (98%) the Scheduler works fine.
Some times , 1%, the scheduler calculates the eligible Array properly, but runs only one module
Sometimes, 1%, the scheduler calculates the eligible Array properly, but does not run any module

Please suggest what could be the reason for this and how to resolve this.

Could this be OS issue, could this be the thread issue? Or Anything else which I am not able to think.

JDK/JRE – 1.06.04

Thanks,
Debasis Guru
  • 1. Re: Inconsistent Behavior of Java Threads...Urgent Help Needed
    EJP Guru
    Currently Being Moderated
    Nowhere in the Javadoc, the Java Language Specification, or the JVM Specification does it give any guarantee about consistent behaviour of this kind. The specification of Thread.sleep() specifically says that a thread will sleep at least as long as specified. There are no real time guarantees in either Java or most consumer operating systems.
  • 2. Re: Inconsistent Behavior of Java Threads...Urgent Help Needed
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    There are no real time guarantees in either Java or most consumer operating systems.
    True but I don't really see anything in the question that actually relates to a problem with sleep. Sometimes the scheduler (which is apparenty hand coded if I read correctly) doesn't do what it is expected, so the code contains a bug, most likely in the code that fills the module array (which is apparently at times not putting a module in there when it should). Perhaps this is the result of a conflicting business rule, who knows without the code to look at.

    There is only one real thing to do here IMO: be a developer and debug your stuff. Debugging thread related code can be difficult, I would suggest trying it with logging first.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points