(thunk-timeout thunk ms)
(thunk-timeout thunk time unit)
(thunk-timeout thunk time unit tg)
Takes a function and an amount of time to wait for thse function to finish
executing. The sandbox can do this for you. unit is any of :ns, :us, :ms,
or :s which correspond to TimeUnit/NANOSECONDS, MICROSECONDS, MILLISECONDS,
and SECONDS respectively.
Source
(defn thunk-timeout
"Takes a function and an amount of time to wait for thse function to finish
executing. The sandbox can do this for you. unit is any of :ns, :us, :ms,
or :s which correspond to TimeUnit/NANOSECONDS, MICROSECONDS, MILLISECONDS,
and SECONDS respectively."
([thunk ms]
(thunk-timeout thunk ms :ms nil)) ; Default to milliseconds, because that's pretty common.
([thunk time unit]
(thunk-timeout thunk time unit nil))
([thunk time unit tg]
(let [task (FutureTask. thunk)
thr (if tg (Thread. tg task) (Thread. task))]
(try
(.start thr)
(.get task time (or (uglify-time-unit unit) unit))
(catch TimeoutException e
(.cancel task true)
(.stop thr)
(throw (TimeoutException. "Execution timed out.")))
(catch Exception e
(.cancel task true)
(.stop thr)
(throw e))
(finally (when tg (.stop tg)))))))