Thread:Sleep apparently shutting down OH2 execution randomly

I’ve already plans to write a looping Timer DP which would cover this. The above code will probably be what I use as the complex example implementation.

A DP is intended to be a generic approach to solve lots of similar problems in a common way. As such it doesn’t make much sense to write a Fan Timer DP. In fact, I really regret naming the Time of Day DP that as it implies that is the only thing it is good for. But the same approach can be used to track any sort of system state which is why I use that Irrigation example for the complex example. But it is really too late to rename it now.

Stay tuned for a looping timer DP though, as that is the meat of the code above.

Running Timers do not count against the Rule thread count. That is one reason why timers are a better choice over Thread:sleep. Now, there is a Timer thread pool (I can’t remember how big it is but I think it is more than five) but one of those threads is only being use when the Timer body is actually running. For example, if you have 50 timers but only two are actually executing the code after going off, only two threads are being used. So in the above a thread will be in use for a small fraction of a second once a minute. That should not cause any problems with running out of threads. Similarly, the Rule itself will only take a fraction of a second to run so you shouldn’t run into Rules running out of threads either.

Edit: Here is part one: (OH 1.x and OH 2.x Rules DSL only] Why have my Rules stopped running? Why Thread::sleep is a bad idea

Part two (DP for the looping timers to follow).

1 Like