Hi,
I'm curious why you're interested so much in stale workflows. A stale workflow means that a workflow in the system with a running state has metadata stored indicating that there should be a sling job executing the workflow. A workflow shows up as stale if the sling job system reports that the job in question is not found in the system. This can sometimes occur in a system that is under high load where a started workflow has been persisted in the repository but the sling job has not yet been persisted. Under these circumstances the workflow will become no longer stale as long as the system is not shutdown. If a workflow has become stale for quite some time restarting it may be the best option as the job will be re-scheduled.
You cannot calculate a stale workflow by a simple JCR query, you must corelate those results with sling jobs. Instead of writing your own code I would suggest using the workflow maintenance jmx bean found here [0]. It has an operation to find out how many stale workflows there are (optionally per model) and has an operation to restart stale workflows.
If you want to simulate stale workflows the best way to do it is pause the workflow sling job queue, run some workflows, then clear the queue. This should delete the jobs that workflow is expecting to be in the system.
As for periodically making this call, you can use the sling scheduler for this, see [0]. It's relatively straightforward to do this and quite powerful, and provides a few options how to build/configure your scheduled job.
Hope this helps,
Will
[0] http://helpx.adobe.com/experience-manager/kb/workflow-monitor-via-jmx.html
[1] http://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html