All bridge children initialized at reboot: how to detect?

Hallo,
in my binding at reboot I need to resynch all things. So what the bridge handler does just after its initialize() is list all things:
List<@NonNull Thing> things = getThing().getThings();
and then iterate over them to call a refreshDevice(all) method on each handler to refresh all things of a certain type (for example refresh all lights, then all thermostats, etc.)

Problem is the list of things returned by bridge.getThings() is not complete yet (probably things are still being loaded from .things file) and/or some thing handlers have not been initialized yet.

So how do I know when all bridge child things handlers have been initialized?

I know there is the childHandlerInitialized method but what i would need to know is when all things have been initialized so that I can finally send the message to the hardware to refresh all the states.
Ideally I would need a allChildHandlerInitialized callback on the bridge…

Short answer: you can‘t. What happens if a thing is added at runtime? You should do the same when childHandlerInitialized is called for a thing.

Why do you want to wait until all handlers are initialized before refreshing?

here I am referring to the special case/situation of boot/reboot.
Of course new things added at runtime after reboot should be treated separately.
Also in the case a new thing/item is added the RefreshType command is already sent by the framework (when an item is linked), but not at reboot, if I am not wrong.

This is the situation: I have 200+ things/items configured via files (or in Db does not matter). If I knew when all handlers are ready I would just send 1 refreshAll protocol message to the hw gateway to get the status of all objects in one shot.
Instead if I had to ask for state refresh for each Thing after its handler it’s initialized, I would need to send 200+ refresh request messages to the hw gateway.
So it’s a matter of optimization at OH boot time to resynch the state on OH with the hw: 200+ msg (sending+processing…) vs 1 msg.

Your solution is the expiringcache. Get the information for all once and let the thing handlers request the information from the bridge on initialization. If you set the expiration time of the cache to 2s or so, this will reduce the amount of refreshes from the gateway.

Here is an example how to do that:

Thank for the suggestion and example.

Right now is not easy to do so in my case as state updates are event based: the bridge requests state for all devices and the hw gateway responds with many message events, one event with the state for each light, this is why I would need to request “all lights” state only when all light handlers have been initialized (so they can receive and process the state message event)

Also in the case of lights a cache of 2s seems a bit dangerous as a light can be activated in the meantime.

I will look a way to treat the case of boot refresh separately.

Are runlevels of any help in this case?
If I had one runlevel telling me “all things have been initialized” I would be fine and send the “all lights” state request only after that run level is reached , but before rules start to be fired.

Is there an example to be notified from code when a run level is reached?