1

Certain regular expressions can consume truly impressive quantities of CPU time. I need to implement a system that controls the total amount of CPU time it will take on a task.

It seems to me that the Java Pattern/Matcher system is not designed to allow me to follow the Java conventions for interrupting execution, as there is no point in the process where the evaluation engine checks for interrupts. According to http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html, one can't call destroy; one's computations have to check for interrupts.

Am I missing some mechanism here, or should I go and find an alternative open source regular expression evaluator for which I can add a time bound?

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • 1
    I have never had a case where regex would take so long I would have to terminate it, but once I had a similar problem when executing some time consuming lookups, what I did is put it in a thread and terminate it after some time. I don't know if this would work with regex, but if you haven't already, you could give it a try. – Marko Gresak Apr 13 '14 at 16:10
  • There is no safe way to terminate a thread. – bmargulies Apr 13 '14 at 16:13
  • @bmargulies: See Thread.interrupt(). Gods know I write enough code to handle that case even though I never expect it to arise in my application... – keshlam Apr 13 '14 at 18:08
  • Thread.interrupt() will not interrupt a call into the regex API AFAIK. – bmargulies Apr 13 '14 at 18:23

0 Answers0