Java7的新特性
Posted 流星<。)#)))≦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java7的新特性相关的知识,希望对你有一定的参考价值。
原文出处:xixicat
序
本文主要讲Java7的新特性,相对于Java6而言,Java7增加了一些重要的特性,比如NIO2,不像Java6那么鸡肋,也算是一个重要的版本。
特性列表
suppress异常(
新语法
)捕获多个异常(新语法)
try-with-resources(新语法)
JSR341-Expression Language Specification(新规范)
JSR203-More New I/O APIs for the Java Platform(新规范)
JSR292与InvokeDynamic
支持JDBC4.1规范
Path接口、DirectoryStream、Files、WatchService
jcmd
fork/join framework
Java Mission Control
1、suppress异常(新语法
)
1 /** 2 * 记录异常,不被淹没 3 * addSuppressed 4 */ 5 class ReadFile { 6 public void read(String filename) throws BaseException { 7 FileInputStream input = null; 8 IOException readException = null; 9 try { 10 input = new FileInputStream(filename); 11 } catch (IOException ex) { 12 readException = ex; 13 } finally { 14 if (input != null) { 15 try { 16 input.close(); 17 } catch (IOException ex) { 18 if (readException == null) { 19 readException = ex; 20 }else{ 21 //使用java7的 22 readException.addSuppressed(ex); 23 } 24 } 25 } 26 if (readException != null) { 27 throw new BaseException(readException); 28 } 29 } 30 } 31 }
2、捕获多个异常(新语法
)
1 public void handle() { 2 ExceptionThrower thrower = new ExceptionThrower(); 3 try { 4 thrower.manyExceptions(); 5 } catch (ExceptionA | ExceptionB ab) { 6 System.out.println(ab.getClass()); 7 } catch (ExceptionC c) { 8 } 9 }
3、try-with-resources(新语法
)
1 /** 2 * try-with-resource 3 * 不需要使用finally来保证打开的流被正确关闭 4 * 这是自动完成的。 5 * @author patterncat 6 * @created 2014-07-21 7 */ 8 public class ResourceBasicUsage { 9 public String readFile(String path) throws IOException { 10 try (BufferedReader reader = new BufferedReader(new FileReader(path))) { 11 StringBuilder builder = new StringBuilder(); 12 String line = null; 13 while ((line = reader.readLine()) != null) { 14 builder.append(line); 15 builder.append(String.format("%n")); 16 } 17 return builder.toString(); 18 } 19 } 20 }
实现AutoCloseable
1 /** 2 * @author patterncat 3 * @created 2014-07-21 4 */ 5 public class CustomResource implements AutoCloseable { 6 public void close() throws Exception { 7 System.out.println("进行资源释放。"); 8 } 9 public void useCustomResource() throws Exception { 10 try (CustomResource resource = new CustomResource()) { 11 System.out.println("使用资源。"); 12 } 13 } 14 public static void main(String[] args) { 15 try { 16 new CustomResource().useCustomResource(); 17 } catch (Exception ex) { 18 ex.printStackTrace(); 19 } 20 } 21 }
4、JSR341-Expression Language Specification(新规范
)
1 public static void main(String[] args){ 2 ELProcessor el = new ELProcessor(); 3 assert (el.eval("Math.random()") instanceof Double); 4 }
5、JSR203-More New I/O APIs for the Java Platform(新规范
)
bytebuffer
1 public class ByteBufferUsage { 2 public void useByteBuffer() { 3 ByteBuffer buffer = ByteBuffer.allocate(32); 4 buffer.put((byte)1); 5 buffer.put(new byte[3]); 6 buffer.putChar(‘A‘); 7 buffer.putFloat(0.0f); 8 buffer.putLong(10, 100L); 9 System.out.println(buffer.getChar(4)); 10 System.out.println(buffer.remaining()); 11 } 12 public void byteOrder() { 13 ByteBuffer buffer = ByteBuffer.allocate(4); 14 buffer.putInt(1); 15 buffer.order(ByteOrder.LITTLE_ENDIAN); 16 buffer.getInt(0); //值为16777216 17 } 18 public void compact() { 19 ByteBuffer buffer = ByteBuffer.allocate(32); 20 buffer.put(new byte[16]); 21 buffer.flip(); 22 buffer.getInt(); 23 buffer.compact(); 24 int pos = buffer.position(); 25 } 26 public void viewBuffer() { 27 ByteBuffer buffer = ByteBuffer.allocate(32); 28 buffer.putInt(1); 29 IntBuffer intBuffer = buffer.asIntBuffer(); 30 intBuffer.put(2); 31 int value = buffer.getInt(); //值为2 32 } 33 /** 34 * @param args the command line arguments 35 */ 36 public static void main(String[] args) { 37 ByteBufferUsage bbu = new ByteBufferUsage(); 38 bbu.useByteBuffer(); 39 bbu.byteOrder(); 40 bbu.compact(); 41 bbu.viewBuffer(); 42 } 43 }
filechannel
1 public class FileChannelUsage { 2 public void openAndWrite() throws IOException { 3 FileChannel channel = FileChannel.open(Paths.get("my.txt"), StandardOpenOption.CREATE, StandardOpenOption.WRITE); 4 ByteBuffer buffer = ByteBuffer.allocate(64); 5 buffer.putChar(‘A‘).flip(); 6 channel.write(buffer); 7 } 8 public void readWriteAbsolute() throws IOException { 9 FileChannel channel = FileChannel.open(Paths.get("absolute.txt"), StandardOpenOption.READ, StandardOpenOption.CREATE, StandardOpenOption.WRITE); 10 ByteBuffer writeBuffer = ByteBuffer.allocate(4).putChar(‘A‘).putChar(‘B‘); 11 writeBuffer.flip(); 12 channel.write(writeBuffer, 1024); 13 ByteBuffer readBuffer = ByteBuffer.allocate(2); 14 channel.read(readBuffer, 1026); 15 readBuffer.flip(); 16 char result = readBuffer.getChar(); //值为‘B‘ 17 } 18 /** 19 * @param args the command line arguments 20 */ 21 public static void main(String[] args) throws IOException { 22 FileChannelUsage fcu = new FileChannelUsage(); 23 fcu.openAndWrite(); 24 fcu.readWriteAbsolute(); 25 } 26 }
6、JSR292与InvokeDynamic
JSR 292: Supporting Dynamically Typed Languages on the JavaTM Platform,支持在JVM上运行动态类型语言。在字节码层面支持了InvokeDynamic。
方法句柄MethodHandle
1 public class ThreadPoolManager { 2 private final ScheduledExecutorService stpe = Executors 3 .newScheduledThreadPool(2); 4 private final BlockingQueue<WorkUnit<String>> lbq; 5 public ThreadPoolManager(BlockingQueue<WorkUnit<String>> lbq_) { 6 lbq = lbq_; 7 } 8 public ScheduledFuture<?> run(QueueReaderTask msgReader) { 9 msgReader.setQueue(lbq); 10 return stpe.scheduleAtFixedRate(msgReader, 10, 10, TimeUnit.MILLISECONDS); 11 } 12 private void cancel(final ScheduledFuture<?> hndl) { 13 stpe.schedule(new Runnable() { 14 public void run() { 15 hndl.cancel(true); 16 } 17 }, 10, TimeUnit.MILLISECONDS); 18 } 19 /** 20 * 使用传统的反射api 21 */ 22 public Method makeReflective() { 23 Method meth = null; 24 try { 25 Class<?>[] argTypes = new Class[]{ScheduledFuture.class}; 26 meth = ThreadPoolManager.class.getDeclaredMethod("cancel", argTypes); 27 meth.setAccessible(true); 28 } catch (IllegalArgumentException | NoSuchMethodException 29 | SecurityException e) { 30 e.printStackTrace(); 31 } 32 return meth; 33 } 34 /** 35 * 使用代理类 36 * @return 37 */ 38 public CancelProxy makeProxy() { 39 return new CancelProxy(); 40 } 41 /** 42 * 使用Java7的新api,MethodHandle 43 * invoke virtual 动态绑定后调用 obj.xxx 44 * invoke special 静态绑定后调用 super.xxx 45 * @return 46 */ 47 public MethodHandle makeMh() { 48 MethodHandle mh; 49 MethodType desc = MethodType.methodType(void.class, ScheduledFuture.class); 50 try { 51 mh = MethodHandles.lookup().findVirtual(ThreadPoolManager.class, 52 "cancel", desc); 53 } catch (NoSuchMethodException | IllegalAccessException e) { 54 throw (AssertionError) new AssertionError().initCause(e); 55 } 56 return mh; 57 } 58 public static class CancelProxy { 59 private CancelProxy() { 60 } 61 public void invoke(ThreadPoolManager mae_, ScheduledFuture<?> hndl_) { 62 mae_.cancel(hndl_); 63 } 64 } 65 }
调用
1 public class ThreadPoolMain { 2 /** 3 * 如果被继承,还能在静态上下文寻找正确的class 4 */ 5 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); 6 private ThreadPoolManager manager; 7 public static void main(String[] args) { 8 ThreadPoolMain main = new ThreadPoolMain(); 9 main.run(); 10 } 11 private void cancelUsingReflection(ScheduledFuture<?> hndl) { 12 Method meth = manager.makeReflective(); 13 try { 14 System.out.println("With Reflection"); 15 meth.invoke(hndl); 16 } catch (IllegalAccessException | IllegalArgumentException 17 | InvocationTargetException e) { 18 e.printStackTrace(); 19 } 20 } 21 private void cancelUsingProxy(ScheduledFuture<?> hndl) { 22 CancelProxy proxy = manager.makeProxy(); 23 System.out.println("With Proxy"); 24 proxy.invoke(manager, hndl); 25 } 26 private void cancelUsingMH(ScheduledFuture<?> hndl) { 27 MethodHandle mh = manager.makeMh(); 28 try { 29 System.out.println("With Method Handle"); 30 mh.invokeExact(manager, hndl); 31 } catch (Throwable e) { 32 e.printStackTrace(); 33 } 34 } 35 private void run() { 36 BlockingQueue<WorkUnit<String>> lbq = new LinkedBlockingQueue<>(); 37 manager = new ThreadPoolManager(lbq); 38 final QueueReaderTask msgReader = new QueueReaderTask(100) { 39 @Override 40 public void doAction(String msg_) { 41 if (msg_ != null) 42 System.out.println("Msg recvd: " + msg_); 43 } 44 }; 45 ScheduledFuture<?> hndl = manager.run(msgReader); 46 cancelUsingMH(hndl); 47 // cancelUsingProxy(hndl); 48 // cancelUsingReflection(hndl); 49 } 50 }
7、支持JDBC4.1规范
abort方法
1 public class AbortConnection { 2 public void abortConnection() throws SQLException { 3 Connection connection = DriverManager 4 .getConnection("jdbc:derby://localhost/java7book"); 5 ThreadPoolExecutor executor = new DebugExecutorService(2, 10, 60, 6 TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); 7 connection.abort(executor); 8 executor.shutdown(); 9 try { 10 executor.awaitTermination(5, TimeUnit.MINUTES); 11 System.out.println(executor.getCompletedTaskCount()); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 private static class DebugExecutorService extends ThreadPoolExecutor { 17 public DebugExecutorService(int corePoolSize, int maximumPoolSize, 18 long keepAliveTime, TimeUnit unit, 19 BlockingQueue<Runnable> workQueue) { 20 super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); 21 } 22 public void beforeExecute(Thread t, Runnable r) { 23 System.out.println("清理任务:" + r.getClass()); 24 super.beforeExecute(t, r); 25 } 26 } 27 public static void main(String[] args) { 28 AbortConnection ca = new AbortConnection(); 29 try { 30 ca.abortConnection(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 } 35 }
自动关闭
1 public class SetSchema { 2 public void setSchema() throws SQLException { 3 try (Connection connection = DriverManager 4 .getConnection("jdbc:derby://localhost/java7book")) { 5 connection.setSchema("DEMO_SCHEMA"); 6 try (Statement stmt = connection.createStatement(); 7 ResultSet rs = stmt.executeQuery("SELECT * FROM author")) { 8 while (rs.next()) { 9 System.out.println(rs.getString("name")); 10 } 11 } 12 } 13 } 14 public static void main(String[] args) { 15 SetSchema ss = new SetSchema(); 16 try { 17 ss.setSchema(); 18 } catch (SQLException e) { 19 e.printStackTrace(); 20 } 21 } 22 }
自动映射
1 public class SetSchema { 2 public void setSchema() throws SQLException { 3 try (Connection connection = DriverManager 4 .getConnection("jdbc:derby://localhost/java7book")) { 5 connection.setSchema("DEMO_SCHEMA"); 6 try (Statement stmt = connection.createStatement(); 7 ResultSet rs = stmt.executeQuery("SELECT * FROM author")) { 8 while (rs.next()) { 9 System.out.println(rs.getString("name")); 10 } 11 } 12 } 13 } 14 public static void main(String[] args) { 15 SetSchema ss = new SetSchema(); 16 try { 17 ss.setSchema(); 18 } catch (SQLException e) { 19 e.printStackTrace(); 20 } 21 } 22 }
8、Path接口(重要接口更新
)
1 public class PathUsage { 2 public void usePath() { 3 Path path1 = Paths.get("folder1", "sub1"); 4 Path path2 = Paths.get("folder2", "sub2"); 5 path1.resolve(path2); //folder1\sub1\folder2\sub2 6 path1.resolveSibling(path2); //folder1\folder2\sub2 7 path1.relativize(path2); //..\..\folder2\sub2 8 path1.subpath(0, 1); //folder1 9 path1.startsWith(path2); //false 10 path1.endsWith(path2); //false 11 Paths.get("folder1/./../folder2/my.text").normalize(); //folder2\my.text 12 } 13 /** 14 * @param args the command line arguments 15 */ 16 public static void main(String[] args) { 17 PathUsage usage = new PathUsage(); 18 usage.usePath(); 19 } 20 }
9、DirectoryStream
1 public class ListFile { 2 public void listFiles() throws IOException { 3 Path path = Paths.get(""); 4 try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.*")) { 5 for (Path entry: stream) { 6 //使用entry 7 System.out.println(entry); 8 } 9 } 10 } 11 /** 12 * @param args the command line arguments 13 */ 14 public static void main(String[] args) throws IOException { 15 ListFile listFile = new ListFile(); 16 listFile.listFiles(); 17 } 18 }
10、Files
1 public class FilesUtils { 2 public void manipulateFiles() throws IOException { 3 Path newFile = Files.createFile(Paths.get("new.txt").toAbsolutePath()); 4 List<String> content = new ArrayList<String>(); 5 content.add("Hello"); 6 content.add("World"); 7 Files.write(newFile, content, Charset.forName("UTF-8")); 8 Files.size(newFile); 9 byte[] bytes = Files.readAllBytes(newFile); 10 ByteArrayOutputStream output = new ByteArrayOutputStream(); 11 Files.copy(newFile, output); 12 Files.delete(newFile); 13 } 14 /** 15 * @param args the command line arguments 16 */ 17 public static void main(String[] args) throws IOException { 18 FilesUtils fu = new FilesUtils(); 19 fu.manipulateFiles(); 20 } 21 }
11、WatchService
1 public class WatchAndCalculate { 2 public void calculate() throws IOException, InterruptedException { 3 WatchService service = FileSystems.getDefault().newWatchService(); 4 Path path = Paths.get("").toAbsolutePath(); 5 path.register(service, StandardWatchEventKinds.ENTRY_CREATE); 6 while (true) { 7 WatchKey key = service.take(); 8 for (WatchEvent<?> event : key.pollEvents()) { 9 Path createdPath = (Path) event.context(); 10 createdPath = path.resolve(createdPath); 11 long size = Files.size(createdPath); 12 System.out.println(createdPath + " ==> " + size); 13 } 14 key.reset(); 15 } 16 } 17 /** 18 * @param args the command line arguments 19 */ 20 public static void main(String[] args) throws Throwable { 21 WatchAndCalculate wc = new WatchAndCalculate(); 22 wc.calculate(); 23 } 24 }
12、jcmd utility
jcmd是为了替代jps出现了,包含了jps的大部分功能并新增了一些新的功能。
jcmd -l
列出所有的Java虚拟机,针对每一个虚拟机可以使用help列出它们支持的命令。
1 jcmd -l 2 15308 org.eclipse.jetty.xml.XmlConfiguration /tmp/start4070493346048555702.properties /opt/educat/apps/conf/jetty8.xml 3 5624 sun.tools.jcmd.JCmd -l 4 20967 org.apache.flume.node.Application --no-reload-conf -f /opt/educat/flume_agent/conf/flume.conf -n log_agent
jcmd pid help
1 jcmd 15308 help 2 15308: 3 The following commands are available: 4 VM.commercial_features 5 ManagementAgent.stop 6 ManagementAgent.start_local 7 ManagementAgent.start 8 Thread.print 9 GC.class_histogram 10 GC.heap_dump 11 GC.run_finalization 12 GC.run 13 VM.uptime 14 VM.flags 15 VM.system_properties 16 VM.command_line 17 VM.version 18 help 19 For more information about a specific command use ‘help <command>‘.
jcmd pid VM.flags查看启动参数
1 jcmd 15308 VM.flags 2 15308: 3 -XX:+DisableExplicitGC 4 -XX:ErrorFile=/var/patterncat/logs/catapp.vmerr.log.201505071655 5 -XX:+HeapDumpOnOutOfMemoryError 6 -XX:HeapDumpPath=/var/patterncat/logs/catapp.heaperr.log.201505071655 -XX:InitialHeapSize=5368709120 7 -XX:+ManagementServer 8 -XX:MaxGCPauseMillis=100 9 -XX:MaxHeapSize=5368709120 10 -XX:MaxPermSize=268435456 11 -XX:+PrintAdaptiveSizePolicy 12 -XX:+PrintCommandLineFlags 13 -XX:+PrintGC 14 -XX:+PrintGCApplicationStoppedTime 15 -XX:+PrintGCDateStamps 16 -XX:+PrintGCDetails 17 -XX:+PrintGCTimeStamps 18 -XX:+PrintHeapAtGC 19 -XX:+PrintTenuringDistribution 20 -XX:StringTableSize=49999 21 -XX:+UnlockExperimentalVMOptions 22 -XX:+UseCompressedOops 23 -XX:+UseG1GC
jcmd pid GC.heap_dump D:\d.dump 导出堆信息
jcmd pid GC.class_histogram查看系统中类的统计信息
jcmd pid VM.system_properties查看系统属性内容
jcmd pid Thread.print 打印线程栈
jcmd pid VM.uptime 查看虚拟机启动时间
jcmd pid PerfCounter.print 查看性能统计
1 jcmd 15308 PerfCounter.print 2 15308: 3 java.ci.totalTime=79326405 4 java.cls.loadedClasses=19977 5 java.cls.sharedLoadedClasses=0 6 java.cls.sharedUnloadedClasses=0 7 java.cls.unloadedClasses=1443 8 java.property.java.class.path="/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-xml-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/servlet-api-3.0.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-http-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-continuation-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-server-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-security-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-servlet-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-webapp-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-deploy-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/lib/jetty-servlets-8.1.9.v20130131.jar:/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131/l" 9 java.property.java.endorsed.dirs="/usr/local/jdk1.7.0_21/jre/lib/endorsed" 10 java.property.java.ext.dirs="/usr/local/jdk1.7.0_21/jre/lib/ext:/usr/java/packages/lib/ext" 11 java.property.java.home="/usr/local/jdk1.7.0_21/jre" 12 java.property.java.library.path="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib" 13 java.property.java.version="1.7.0_21" 14 java.property.java.vm.info="mixed mode" 15 java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM" 16 java.property.java.vm.specification.name="Java Virtual Machine Specification" 17 java.property.java.vm.specification.vendor="Oracle Corporation" 18 java.property.java.vm.specification.version="1.7" 19 java.property.java.vm.vendor="Oracle Corporation" 20 java.property.java.vm.version="23.21-b01" 21 java.rt.vmArgs="-javaagent:/opt/educat/apps/lib/jolokia-jvm-1.1.0-agent.jar=port=23061 -Xloggc:/var/patterncat/logs/catapp.gc.log.201505071655 -XX:ErrorFile=/var/patterncat/logs/catapp.vmerr.log.201505071655 -XX:HeapDumpPath=/var/patterncat/logs/catapp.heaperr.log.201505071655 -Xmx5g -Xms5g -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintCommandLineFlags -XX:+PrintAdaptiveSizePolicy -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:StringTableSize=49999 -Djetty.home=/opt/educat/apps/server/jetty-distribution-8.1.9.v20130131 -Dapp.port=8061 -Dmedis_environment=online -Dcore.step=app -DSTOP.PORT=38061 -Djetty.port=8061 -Dcom.sun.management.jmxremote.authenticate=false -Dapp.logdir=/var/patterncat/logs -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -Dapp.ip=10.64.28.207 -Dapp.cont" 22 java.rt.vmFlags="" 23 java.threads.daemon=72 24 java.threads.live=128 25 java.threads.livePeak=129 26 java.threads.started=1444 27 sun.ci.compilerThread.0.compiles=2595 28 sun.ci.compilerThread.0.method="" 29 sun.ci.compilerThread.0.time=1290 30 sun.ci.compilerThread.0.type=1 31 sun.ci.compilerThread.1.compiles=2802 32 sun.ci.compilerThread.1.method="" 33 sun.ci.compilerThread.1.time=1413 34 sun.ci.compilerThread.1.type=2 35 sun.ci.lastFailedMethod="" 36 sun.ci.lastFailedType=0 37 sun.ci.lastInvalidatedMethod="" 38 sun.ci.lastInvalidatedType=0 39 sun.ci.lastMethod="org/codehaus/groovy/classgen/VariableScopeVisitor checkVariableNameForDeclaration" 40 sun.ci.lastSize=2184 41 sun.ci.lastType=1 42 sun.ci.nmethodCodeSize=12188576 43 sun.ci.nmethodSize=24492688 44 sun.ci.osrBytes=196694 45 sun.ci.osrCompiles=156 46 sun.ci.osrTime=8521713 47 sun.ci.standardBytes=2072839 48 sun.ci.standardCompiles=5241 49 sun.ci.standardTime=70804692 50 sun.ci.threads=2 51 sun.ci.totalBailouts=0 52 sun.ci.totalCompiles=5397 53 sun.ci.totalInvalidates=0 54 sun.classloader.findClassTime=358334873 55 sun.classloader.findClasses=507 56 sun.classloader.parentDelegationTime=30062667 57 sun.cls.appClassBytes=63743816 58 sun.cls.appClassLoadCount=58098 59 sun.cls.appClassLoadTime=9843833 60 sun.cls.appClassLoadTime.self=5288490 61 sun.cls.classInitTime=2617049 62 sun.cls.classInitTime.self=1088905 63 sun.cls.classLinkedTime=4605704 64 sun.cls.classLinkedTime.self=541928 65 sun.cls.classVerifyTime=4055324 66 sun.cls.classVerifyTime.self=2423448 67 sun.cls.defineAppClassTime=3206202 68 sun.cls.defineAppClassTime.self=386302 69 sun.cls.defineAppClasses=16465 70 sun.cls.initializedClasses=14546 71 sun.cls.isUnsyncloadClassSet=0 72 sun.cls.jniDefineClassNoLockCalls=94 73 sun.cls.jvmDefineClassNoLockCalls=4405 74 sun.cls.jvmFindLoadedClassNoLockCalls=32671 75 sun.cls.linkedClasses=16465 76 sun.cls.loadInstanceClassFailRate=0 77 sun.cls.loadedBytes=43314456 78 sun.cls.lookupSysClassTime=87247 79 sun.cls.methodBytes=34262690 80 sun.cls.nonSystemLoaderLockContentionRate=133 81 sun.cls.parseClassTime=3099390 82 sun.cls.parseClassTime.self=2670584 83 sun.cls.sharedClassLoadTime=9647 84 sun.cls.sharedLoadedBytes=0 85 sun.cls.sharedUnloadedBytes=0 86 sun.cls.sysClassBytes=12986737 87 sun.cls.sysClassLoadTime=503885 88 sun.cls.systemLoaderLockContentionRate=0 89 sun.cls.time=15382336 90 sun.cls.unloadedBytes=5087120 91 sun.cls.unsafeDefineClassCalls=1555 92 sun.cls.verifiedClasses=16383 93 sun.gc.cause="No GC" 94 sun.gc.collector.0.invocations=85 95 sun.gc.collector.0.lastEntryTime=24164511065 96 sun.gc.collector.0.lastExitTime=24164628388 97 sun.gc.collector.0.name="G1 incremental collections" 98 sun.gc.collector.0.time=7628099 99 sun.gc.collector.1.invocations=1 100 sun.gc.collector.1.lastEntryTime=24543200515 101 sun.gc.collector.1.lastExitTime=24544107869 102 sun.gc.collector.1.name="G1 stop-the-world full collections" 103 sun.gc.collector.1.time=907355 104 sun.gc.generation.0.agetable.bytes.00=0 105 sun.gc.generation.0.agetable.bytes.01=4294976 106 sun.gc.generation.0.agetable.bytes.02=2014880 107 sun.gc.generation.0.agetable.bytes.03=5406352 108 sun.gc.generation.0.agetable.bytes.04=4875176 109 sun.gc.generation.0.agetable.bytes.05=2865952 110 sun.gc.generation.0.agetable.bytes.06=4374048 111 sun.gc.generation.0.agetable.bytes.07=2058664 112 sun.gc.generation.0.agetable.bytes.08=3574376 113 sun.gc.generation.0.agetable.bytes.09=6923448 114 sun.gc.generation.0.agetable.bytes.10=1541088 115 sun.gc.generation.0.agetable.bytes.11=1347376 116 sun.gc.generation.0.agetable.bytes.12=735888 117 sun.gc.generation.0.agetable.bytes.13=402632 118 sun.gc.generation.0.agetable.bytes.14=713272 119 sun.gc.generation.0.agetable.bytes.15=728688 120 sun.gc.generation.0.agetable.size=16 121 sun.gc.generation.0.capacity=4510973976 122 sun.gc.generation.0.maxCapacity=5368709144 123 sun.gc.generation.0.minCapacity=24 124 sun.gc.generation.0.name="young" 125 sun.gc.generation.0.space.0.capacity=4510973960 126 sun.gc.generation.0.space.0.initCapacity=1128267784 127 sun.gc.generation.0.space.0.maxCapacity=5368709128 128 sun.gc.generation.0.space.0.name="eden" 129 sun.gc.generation.0.space.0.used=580911104 130 sun.gc.generation.0.space.1.capacity=8 131 sun.gc.generation.0.space.1.initCapacity=8 132 sun.gc.generation.0.space.1.maxCapacity=8 133 sun.gc.generation.0.space.1.name="s0" 134 sun.gc.generation.0.space.1.used=0 135 sun.gc.generation.0.space.2.capacity=8 136 sun.gc.generation.0.space.2.initCapacity=8 137 sun.gc.generation.0.space.2.maxCapacity=5368709128 138 sun.gc.generation.0.space.2.name="s1" 139 sun.gc.generation.0.space.2.used=0 140 sun.gc.generation.0.spaces=3 141 sun.gc.generation.1.capacity=857735176 142 sun.gc.generation.1.maxCapacity=5368709128 143 sun.gc.generation.1.minCapacity=8 144 sun.gc.generation.1.name="old" 145 sun.gc.generation.1.space.0.capacity=857735176 146 sun.gc.generation.1.space.0.initCapacity=4240441352 147 sun.gc.generation.1.space.0.maxCapacity=5368709128 148 sun.gc.generation.1.space.0.name="space" 149 sun.gc.generation.1.space.0.used=155730608 150 sun.gc.generation.1.spaces=1 151 sun.gc.generation.2.capacity=138412032 152 sun.gc.generation.2.maxCapacity=268435456 153 sun.gc.generation.2.minCapacity=20971520 154 sun.gc.generation.2.name="perm" 155 sun.gc.generation.2.space.0.capacity=138412032 156 sun.gc.generation.2.space.0.initCapacity=20971520 157 sun.gc.generation.2.space.0.maxCapacity=268435456 158 sun.gc.generation.2.space.0.name="perm" 159 sun.gc.generation.2.space.0.used=138212560 160 sun.gc.generation.2.spaces=1 161 sun.gc.lastCause="Heap Inspection Initiated GC" 162 sun.gc.policy.collectors=1 163 sun.gc.policy.desiredSurvivorSize=264241152 164 sun.gc.policy.generations=3 165 sun.gc.policy.maxTenuringThreshold=15 166 sun.gc.policy.name="GarbageFirst" 167 sun.gc.policy.tenuringThreshold=15 168 sun.gc.tlab.alloc=0 169 sun.gc.tlab.allocThreads=0 170 sun.gc.tlab.fastWaste=0 171 sun.gc.tlab.fills=0 172 sun.gc.tlab.gcWaste=0 173 sun.gc.tlab.maxFastWaste=0 174 sun.gc.tlab.maxFills=0 175 sun.gc.tlab.maxGcWaste=0 176 sun.gc.tlab.maxSlowAlloc=0 177 sun.gc.tlab.maxSlowWaste=0 178 sun.gc.tlab.slowAlloc=0 179 sun.gc.tlab.slowWaste=0 180 sun.management.JMXConnectorServer.0.authenticate="false" 181 sun.management.JMXConnectorServer.0.remoteAddress="service:jmx:rmi:///jndi/rmi://edu-cat02.lf.patterncat.com:8199/jmxrmi" 182 sun.management.JMXConnectorServer.0.ssl="false" 183 sun.management.JMXConnectorServer.0.sslNeedClientAuth="false" 184 sun.management.JMXConnectorServer.0.sslRegistry="false" 185 sun.management.JMXConnectorServer.address="service:jmx:rmi://127.0.0.1/stub/rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LnJlbW90ZS5ybWkuUk1JU2VydmVySW1wbF9TdHViAAAAAAAAAAICAAB4cgAaamF2YS5ybWkuc2VydmVyLlJlbW90ZVN0dWLp/tzJi+FlGgIAAHhyABxqYXZhLnJtaS5zZXJ2ZXIuUmVtb3RlT2JqZWN002G0kQxhMx4DAAB4cHc3AAtVbmljYXN0UmVmMgAADDEwLjY0LjI4LjIwNwAAhbjWmVwaDwiNg3l3YeUAAAFNLZX68oACAHg=" 186 sun.os.hrt.frequency=1000000 187 sun.os.hrt.ticks=24580753795 188 sun.perfdata.majorVersion=2 189 sun.perfdata.minorVersion=0 190 sun.perfdata.overflow=0 191 sun.perfdata.size=32768 192 sun.perfdata.timestamp=259316 193 sun.perfdata.used=17792 194 sun.property.sun.boot.class.path="/usr/local/jdk1.7.0_21/jre/lib/resources.jar:/usr/local/jdk1.7.0_21/jre/lib/rt.jar:/usr/local/jdk1.7.0_21/jre/lib/sunrsasign.jar:/usr/local/jdk1.7.0_21/jre/lib/jsse.jar:/usr/local/jdk1.7.0_21/jre/lib/jce.jar:/usr/local/jdk1.7.0_21/jre/lib/charsets.jar:/usr/local/jdk1.7.0_21/jre/lib/jfr.jar:/usr/local/jdk1.7.0_21/jre/classes" 195 sun.property.sun.boot.library.path="/usr/local/jdk1.7.0_21/jre/lib/amd64" 196 sun.rt._sync_ContendedLockAttempts=297851 197 sun.rt._sync_Deflations=438863 198 sun.rt._sync_EmptyNotifications=0 199 sun.rt._sync_FailedSpins=0 200 sun.rt._sync_FutileWakeups=349651 201 sun.rt._sync_Inflations=438971 202 sun.rt._sync_MonExtant=16256 203 sun.rt._sync_MonInCirculation=0 204 sun.rt._sync_MonScavenged=0 205 sun.rt._sync_Notifications=1580811 206 sun.rt._sync_Parks=1935145 207 sun.rt._sync_PrivateA=0 208 sun.rt._sync_PrivateB=0 209 sun.rt._sync_SlowEnter=0 210 sun.rt._sync_SlowExit=0 211 sun.rt._sync_SlowNotify=0 212 sun.rt._sync_SlowNotifyAll=0 213 sun.rt._sync_SuccessfulSpins=0 214 sun.rt.applicationTime=24559855809 215 sun.rt.createVmBeginTime=1430988913170 216 sun.rt.createVmEndTime=1430988913429 217 sun.rt.internalVersion="Java HotSpot(TM) 64-Bit Server VM (23.21-b01) for linux-amd64 JRE (1.7.0_21-b11), built on Apr 4 2013 04:03:29 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)" 218 sun.rt.interruptedBeforeIO=0 219 sun.rt.interruptedDuringIO=0 220 sun.rt.javaCommand="org.eclipse.jetty.xml.XmlConfiguration /tmp/start4070493346048555702.properties /opt/educat/apps/conf/jetty8.xml" 221 sun.rt.jvmCapabilities="1000000000000000000000000000000000000000000000000000000000000000" 222 sun.rt.jvmVersion=387252225 223 sun.rt.safepointSyncTime=2333795 224 sun.rt.safepointTime=15955181 225 sun.rt.safepoints=18365 226 sun.rt.threadInterruptSignaled=0 227 sun.rt.vmInitDoneTime=1430988913232 228 sun.threads.vmOperationTime=9516621 229 sun.urlClassLoader.readClassBytesTime=958824201 230 sun.zip.zipFile.openTime=72163038 231 sun.zip.zipFiles=3838
13、fork/join
Java7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架。
14、Java Mission Control
在JDK7u40里头提供了Java Mission Control,这个是从JRockit虚拟机里头迁移过来的类似JVisualVm的东东。
15、其他
Binary Literals支持
Numeric Literals的下划线支持
Strings in switch Statements
参考
以上是关于Java7的新特性的主要内容,如果未能解决你的问题,请参考以下文章