涉及到线程安全的部分出现了JVM BUG——不解,Eclipse返回的信息如下,
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:3161), pid=1116, tid=3912
# Error: ShouldNotReachHere()
#
# JRE version: 6.0_21-b07
# Java VM: Java HotSpot(TM) Client VM (17.0-b17 mixed mode windows-x86 )
# An error report file with more information is saved as:
# D:\user\workspace\MultiThread\hs_err_pid1116.log
MultiThread\hs_err_pid1116.log的内容:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:3161), pid=1116, tid=3912
# Error: ShouldNotReachHere()
#
# JRE version: 6.0_21-b07
# Java VM: Java HotSpot(TM) Client VM (17.0-b17 mixed mode windows-x86 )
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
--------------- T H R E A D ---------------
Current thread (0x01ad9000): JavaThread "Unknown thread" [_thread_in_vm, id=3912, stack(0x00260000,0x002b0000)]
Stack: [0x00260000,0x002b0000], sp=0x002af880, free space=13e002af890k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V [jvm.dll+0x1fd090]
V [jvm.dll+0xa47c9]
V [jvm.dll+0x75f01]
V [jvm.dll+0x775a4]
V [jvm.dll+0x1cbcb5]
V [jvm.dll+0x1cc3ed]
V [jvm.dll+0x1cc7a5]
V [jvm.dll+0x1cc7eb]
V [jvm.dll+0x1cc912]
V [jvm.dll+0x1cc98a]
V [jvm.dll+0x1cce3d]
V [jvm.dll+0x1cd252]
V [jvm.dll+0x1eceb3]
V [jvm.dll+0x1ed28c]
V [jvm.dll+0xdb721]
V [jvm.dll+0x1e6618]
V [jvm.dll+0x1065d0]
C [javaw.exe+0x1657]
C [javaw.exe+0x1e2c]
C [javaw.exe+0x8614]
C [kernel32.dll+0x4ed6c]
C [ntdll.dll+0x6377b]
C [ntdll.dll+0x6374e]
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
Other Threads:
=>0x01ad9000 (exited) JavaThread "Unknown thread" [_thread_in_vm, id=3912, stack(0x00260000,0x002b0000)]
VM state:not at safepoint (not fully initialized)
VM Mutex/Monitor currently owned by a thread: None
Dynamic libraries:
0x00400000 - 0x00424000 C:\Program Files\Java\jdk1.6.0_21\bin\javaw.exe
0x775f0000 - 0x7772c000 C:\Windows\SYSTEM32\ntdll.dll
0x76bd0000 - 0x76ca4000 C:\Windows\system32\kernel32.dll
0x75860000 - 0x758aa000 C:\Windows\system32\KERNELBASE.dll
0x77430000 - 0x774d0000 C:\Windows\system32\ADVAPI32.dll
0x76d90000 - 0x76e3c000 C:\Windows\system32\msvcrt.dll
0x75c20000 - 0x75c39000 C:\Windows\SYSTEM32\sechost.dll
0x75a80000 - 0x75b21000 C:\Windows\system32\RPCRT4.dll
0x77360000 - 0x77429000 C:\Windows\system32\USER32.dll
0x75da0000 - 0x75dee000 C:\Windows\system32\GDI32.dll
0x777b0000 - 0x777ba000 C:\Windows\system32\LPK.dll
0x772c0000 - 0x7735d000 C:\Windows\system32\USP10.dll
0x77760000 - 0x7777f000 C:\Windows\system32\IMM32.DLL
0x75df0000 - 0x75ebc000 C:\Windows\system32\MSCTF.dll
0x7c340000 - 0x7c396000 C:\Program Files\Java\jdk1.6.0_21\jre\bin\msvcr71.dll
0x6d8b0000 - 0x6db57000 C:\Program Files\Java\jdk1.6.0_21\jre\bin\client\jvm.dll
0x73c80000 - 0x73cb2000 C:\Windows\system32\WINMM.dll
0x756b0000 - 0x756fc000 C:\Windows\system32\apphelp.dll
0x6d860000 - 0x6d86c000 C:\Program Files\Java\jdk1.6.0_21\jre\bin\verify.dll
0x6d3e0000 - 0x6d3ff000 C:\Program Files\Java\jdk1.6.0_21\jre\bin\java.dll
0x6d340000 - 0x6d348000 C:\Program Files\Java\jdk1.6.0_21\jre\bin\hpi.dll
0x77750000 - 0x77755000 C:\Windows\system32\PSAPI.DLL
0x6d8a0000 - 0x6d8af000 C:\Program Files\Java\jdk1.6.0_21\jre\bin\zip.dll
VM Arguments:
jvm_args: -Dfile.encoding=UTF-8 -Xbootclasspath:C:\Program Files\Android\android-sdk\platforms\android-14\android.jar
java_command: com.gogler.MultiThread.ThreadSafeDemo
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_21
CLASSPATH=.;C:\Program Files\Java\jdk1.6.0_21\lib\tools.jar;C:\Program Files\Java\jdk1.6.0_21\lib\dt.jar;C:\Program Files\Java\jdk1.6.0_21\bin;
PATH=C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\Program Files\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program
Files\TortoiseSVN\bin;C:\Program Files\Java\jdk1.6.0_21\bin;F:\android-sdk-windows\android-sdk-windows\tools;F:\android-sdk-windows\android-sdk-windows\platform-tools;d:\Program Files\DBank\ClickUp;C:\Users\Administrator\Downloads\eclipse-SDK-3.7-win32\eclipse;
USERNAME=Administrator
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 10, GenuineIntel
--------------- S Y S T E M ---------------
OS: Windows 7 Build 7601 Service Pack 1
CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 23 stepping 10, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3
Memory: 4k page, physical 3634232k(2249368k free), swap 7266708k(5328364k free)
vm_info: Java HotSpot(TM) Client VM (17.0-b17) for windows-x86 JRE (1.6.0_21-b07), built on Jul 17 2010 01:10:15 by "java_re" with MS VC++ 7.1 (VS2003)
time: Sat Apr 28 17:11:19 2012
elapsed time: 0 seconds
执行的代码:
package com.gogler.MultiThread; //Java语言: ArrayList的线程安全与不安全对比演示 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class ThreadSafeDemo { public static int demo(final List list, final int testCount) throws InterruptedException { ThreadGroup group = new ThreadGroup(list.getClass().getName() + "@" + list.hashCode()); final Random rand = new Random(); Runnable listAppender = new Runnable() { public void run() { try { Thread.sleep(rand.nextInt(2)); } catch (InterruptedException e) { return; } list.add("0"); } }; for (int i = 0; i < testCount; i++) { new Thread(group, listAppender, "InsertList-" + i).start(); } while (group.activeCount() > 0) { Thread.sleep(10); } return list.size(); } public static void main(String[] args) throws InterruptedException { List unsafeList = new ArrayList(); List safeList = Collections.synchronizedList(new ArrayList()); final int N = 10000; for (int i = 0; i < 10; i++) { unsafeList.clear(); safeList.clear(); int unsafeSize = demo(unsafeList, N); int safeSize = demo(safeList, N); System.out.println("unsafe/safe: " + unsafeSize + "/" + safeSize); } } }