InterruptibleRegexExample.java public class InterruptibleRegexExample { public static void main(String[] args) throws InterruptedException { final Pattern pattern = Pattern.compile("(0*)*A"); final String input = "00000000000000000000000000"; Runnable runnable = new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); Matcher interruptableMatcher = pattern.matcher(new InterruptibleCharSequence(input)); interruptableMatcher.find(); // runs for a long time! System.out.println("Regex took:" + (System.currentTimeMillis() - startTime) + "ms"); } }; Thread thread = new Thread(runnable); thread.start(); Thread.sleep(500); thread.interrupt(); } } //源代码片段来自云代码http://yuncode.net