Using Thread to wait until some condition happens

Dear Community,

I want to take your help please.

I have two rules:

Rule1: populates items states from files when System started.
Rule2: also trigger when System started however it depends on values which are populated by rule1

When I run the system. sometimes it issues errors because rule2 runs before rule1.

What do you suggest to solve this issue: using Thread, Timer, …?


Also, here I tried test rule, in which I believe I will get the log “Thread test - inside while:” many times and log “Thread test - outside while:” once.
But I never get “Thread test - outside while:” ?

var int c = 0
var int d = 0
rule "test rule"
when
    System started
then
    while(Test_switch.state == ON){
        Thread::sleep(1000)
        logInfo("Temp", "Thread test - inside while: " + c++)                                 
    }

    logInfo("Temp", "Thread test - outside while: " + d++)                                 
end

As Items start out as NULL, did you mean
while(Test_switch.state != ON)

yes

Aren’t you reinventing the wheel?
The peraistence-service MapDB is build for the strategy “restoreOnStartUp”. Sounds like you are trying to do the same.

1 Like

This is dangerous and could lock a thread indefinitely…
Please see:

There is a suggestion for running a while loop with a timer instead of locking up a rule:

Why? As opus states, this is the job of Persistence and the restoreOnStartup strategy. Theoretically (there is a bug right now that makes what I’m about to write not always true), your Items should be restored to their state before any of your rules start running. So there is no need to wait.

Again why? A better approach would be to have only one Rule and the first half of the Rule load your values from the file and the second half have the code that depends on those values having been loaded.

You should not have more than one Rule that has the same trigger if they are actually independent. Since these two Rules are dependent on each other, it is better to combine them into the same Rule. If you are an experienced programmer, this is one of the many areas you will have frustration with the Rules DSL. If you are an experienced developer I would recommend the JSR223 Rules that will let you write Rules in a less constrained language like Jython, JavaScript, or Groovy.

Rules are not functions. Problems will occur if you try to treat them as if they were. Rules encapsulate the actions to take place when a given event occurs.

Now, there are work arounds including (in order of preference):

But Rule runtime threads are a limited resource. There are only five in the thread pool by default. This means you can only run up to five Rules at the same time. And if you have a Rule that potentially never returns, like Vincent said, that makes that thread permanently unavailable. If you run out of all five threads no Rules can run. So you should avoid at all costs any Rules that can potentially never return like an indeterminate while loop. Design Pattern: Looping Timers is a better approach because the Rule doesn’t sit around consuming a runtime thread doing nothing freeing up the thread for other Rules to use.

Exactly what others are thinking here, what are you trying to achieve ?

Also really curious what files are you pullin data from?