循環使用正則により、プロセスの保留とCPU使用率が100%高くなります.

6975 ワード

循環使用正則により、プロセスの保留とCPU使用率が100%高くなります.
次のコードはテストコードです.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ErrorDataStatTest {
	public static Map<String, EntryBean> errorMap = new HashMap<String, EntryBean>();

	public static void main(String[] args) {
		String dest = "/Users/kevin/Documents/shop.do1";
		try {
			new ErrorDataStatTest().statData(new File(dest));
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (Map.Entry<String, EntryBean> entry : errorMap.entrySet()) {
			System.out.println(entry.getKey() + "\t" + entry.getValue().val + "\t" + entry.getValue().num);
		}
	}

	public static class EntryBean {
		String key;
		String val;
		int num = 0;

		public EntryBean(String key, String val) {
			this.key = key;
			this.val = val;
		}

		public void add() {
			this.num++;
		}
	}

	public static final String regex_code = ".*?code.*?=.*?\"(-?[\\d]*)\"";
	public static final Pattern pattern = Pattern.compile(regex_code);
	public static final String regex_code_1 = ".*?Code\\s?=\\s?(-?[\\d]*).*?";
	public static final Pattern pattern_1 = Pattern.compile(regex_code_1);
	// Code
	private static String parserErrorCode(String error) {
		Matcher matcher = pattern.matcher(error);
		if (matcher.find()) {
			return matcher.group(1);
		}
		Matcher matcher1 = pattern_1.matcher(error);
		if (matcher1.find()) {
			return matcher1.group(1);
		}
		return null;
	}

	public static final String regex_desc = ".*?description.*?=.*?\"(.*?)\"";
	public static final Pattern pattern_desc = Pattern.compile(regex_desc);
	private static final String regex_desc_1 = ".*?Code\\s?=\\s?[-]?[\\d]*\\s*\"(.*?)\".*?";
	private static final Pattern pattern_desc_1 = Pattern.compile(regex_desc_1);
	// 
	private static String parserErrorDesc(String error) {
		Matcher matcher = pattern_desc.matcher(error);
		if (matcher.find()) {
			return matcher.group(1);
		}
		Matcher matcher1 = pattern_desc_1.matcher(error);
		if (matcher1.find()) {
			return matcher1.group(1);
		}
		return null;
	}

	private String process(String str) {
		if (str != null) {
			String key = parserErrorCode(str), val = parserErrorDesc(str);
			EntryBean entryBean = errorMap.get(key);
			if (entryBean == null) {
				entryBean = new EntryBean(key, val);
			} else {
				entryBean.add();
			}
			errorMap.put(key, entryBean);
		}
		return str;
	}

	/* : */
	protected void statData(File srcFile) throws IOException {
		FileReader reader = new FileReader(srcFile);
		BufferedReader bReader = new BufferedReader(reader);
		try {
			String line = process(bReader.readLine());
			int num = 0;
			while (line != null) {
				line = process(bReader.readLine());
				System.out.println(num);
				num++;
			}

		} catch (FileNotFoundException ex) {
			throw ex;
		} finally {
			bReader.close();
			bReader = null;
			reader.close();
			reader = null;
		}
	}
}

ローカルテストは何度もループ処理が11497回まで行われている間にプログラムが動かず、プロセスが停止し、CPU消費が100%程度でダウンしない.
localhost:~ kevin$ jstack 675
2014-03-22 11:10:16
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode):
"Attach Listener"daemon prio=5 tid=0x00007f96638e9000 nid=0x3c0b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
"Service Thread"daemon prio=5 tid=0x00007f966301e000 nid=0x4b03 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1"daemon prio=5 tid=0x00007f966301d000 nid=0x4903 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0"daemon prio=5 tid=0x00007f966300d000 nid=0x4703 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
"Signal Dispatcher"daemon prio=5 tid=0x00007f9663014000 nid=0x4503 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
"Finalizer"daemon prio=5 tid=0x00007f9664827000 nid=0x3103 in Object.wait() [0x000000011108c000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007eaa85568> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000007eaa85568> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189)
"Reference Handler"daemon prio=5 tid=0x00007f9664029000 nid=0x2f03 in Object.wait() [0x0000000110f89000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007eaa850f0> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:503)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
- locked <0x00000007eaa850f0> (a java.lang.ref.Reference$Lock)
"main"prio=5 tid=0x00007f9664800000 nid=0x1903 runnable [0x0000000109753000]
   java.lang.Thread.State: RUNNABLE
at java.util.regex.Pattern$Curly.match1(Pattern.java:4198)
at java.util.regex.Pattern$Curly.match(Pattern.java:4134)
at java.util.regex.Pattern$Start.match(Pattern.java:3408)
at java.util.regex.Matcher.search(Matcher.java:1199)
at java.util.regex.Matcher.find(Matcher.java:592)
at com.ishopping.sign.controller.ErrorDataStatTest.parserErrorDesc(ErrorDataStatTest.java:72)
at com.ishopping.sign.controller.ErrorDataStatTest.process(ErrorDataStatTest.java:84)
at com.ishopping.sign.controller.ErrorDataStatTest.statData(ErrorDataStatTest.java:140)
at com.ishopping.sign.controller.ErrorDataStatTest.main(ErrorDataStatTest.java:24)
"VM Thread"prio=5 tid=0x00007f9664028800 nid=0x2d03 runnable 
"GC task thread#0 (ParallelGC)"prio=5 tid=0x00007f966300f800 nid=0x2503 runnable 
"GC task thread#1 (ParallelGC)"prio=5 tid=0x00007f9663010800 nid=0x2703 runnable 
"GC task thread#2 (ParallelGC)"prio=5 tid=0x00007f9663011000 nid=0x2903 runnable 
"GC task thread#3 (ParallelGC)"prio=5 tid=0x00007f9663011800 nid=0x2b03 runnable 
"VM Periodic Task Thread"prio=5 tid=0x00007f9663019800 nid=0x4d03 waiting on condition 
JNI global references: 181
ORACLE:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507およびhttp://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6988218
#まとめ
高周波で実行されるコードや高同時性のシステムでは、できるだけ正則を使用しないことをお勧めします.