How to set a time limit on a java function running a regex

后端 未结 10 937
难免孤独
难免孤独 2020-12-17 02:41

I am running a regex in a java function to parse a document and return true if it has found the string specified by the regex and return false if it hasn\'t. But the problem

10条回答
  •  清酒与你
    2020-12-17 03:22

    What you've done kind of looks fine to me here's how I'd modify it:

    final AtomicReference resultXml = new AtomicReference();
    
    RegexpThread rt = new RegexpThread() {
      public void run() {
        method2(m, urlCopy, document, resultXml);
      }
    
    };
    
    rt.start();
    
    try {
        rt.join(6 * 1000);
    } catch (InterruptedException e) {
        return "y";
    }
    
    if(resultXml.get() == null) {
        rt.interupt();
        return "g";
    }
    
    resultXml.append(resultXml.get());
    
    return resultXml.toString();
    

提交回复
热议问题