Java5的新特性
- 泛型
- 枚舉
- 裝箱拆箱
- 變長參數(shù)
- 注解
- foreach循環(huán)
- 靜態(tài)導(dǎo)入
- 格式化
- 線程框架/數(shù)據(jù)結(jié)構(gòu)
- Arrays工具類/StringBuilder/instrument
1葱色、泛型
所謂類型擦除指的就是Java源碼中的范型信息只允許停留在編譯前期,而編譯后的字節(jié)碼文件中將不再保留任何的范型信息娘香。也就是說苍狰,范型信息在編譯時將會被全部刪除办龄,其中范型類型的類型參數(shù)則會被替換為Object類型,并在實(shí)際使用時強(qiáng)制轉(zhuǎn)換為指定的目標(biāo)數(shù)據(jù)類型淋昭。而C++中的模板則會在編譯時將模板類型中的類型參數(shù)根據(jù)所傳遞的指定數(shù)據(jù)類型生成相對應(yīng)的目標(biāo)代碼俐填。
Map<Integer, Integer> squares = new HashMap<Integer, Integer>();
- 通配符類型:避免unchecked警告,問號表示任何類型都可以接受
public void printList(List<?> list, PrintStream out) throws IOException {
for (Iterator<?> i = list.iterator(); i.hasNext(); ) {
out.println(i.next().toString());
}
}
- 限制類型
public static <A extends Number> double sum(Box<A> box1,Box<A> box2){
double total = 0;
for (Iterator<A> i = box1.contents.iterator(); i.hasNext(); ) {
total = total + i.next().doubleValue();
}
for (Iterator<A> i = box2.contents.iterator(); i.hasNext(); ) {
total = total + i.next().doubleValue();
}
return total;
}
2翔忽、枚舉
- EnumMap
public void testEnumMap(PrintStream out) throws IOException {
// Create a map with the key and a String message
EnumMap<AntStatus, String> antMessages =
new EnumMap<AntStatus, String>(AntStatus.class);
// Initialize the map
antMessages.put(AntStatus.INITIALIZING, "Initializing Ant...");
antMessages.put(AntStatus.COMPILING, "Compiling Java classes...");
antMessages.put(AntStatus.COPYING, "Copying files...");
antMessages.put(AntStatus.JARRING, "JARring up files...");
antMessages.put(AntStatus.ZIPPING, "ZIPping up files...");
antMessages.put(AntStatus.DONE, "Build complete.");
antMessages.put(AntStatus.ERROR, "Error occurred.");
// Iterate and print messages
for (AntStatus status : AntStatus.values() ) {
out.println("For status " + status + ", message is: " +
antMessages.get(status));
}
}
- switch枚舉
public String getDescription() {
switch(this) {
case ROSEWOOD: return "Rosewood back and sides";
case MAHOGANY: return "Mahogany back and sides";
case ZIRICOTE: return "Ziricote back and sides";
case SPRUCE: return "Sitka Spruce top";
case CEDAR: return "Wester Red Cedar top";
case AB_ROSETTE: return "Abalone rosette";
case AB_TOP_BORDER: return "Abalone top border";
case IL_DIAMONDS:
return "Diamonds and squares fretboard inlay";
case IL_DOTS:
return "Small dots fretboard inlay";
default: return "Unknown feature";
}
}
3英融、Autoboxing與Unboxing
將primitive類型轉(zhuǎn)換成對應(yīng)的wrapper類型:Boolean、Byte歇式、Short驶悟、Character、Integer材失、Long痕鳍、Float、Double
public static void m1(Integer i){
System.out.println("this is integer");
}
public static void m1(double d){
System.out.println("this is double");
}
m1(1)輸出的是double龙巨,方法匹配時線下兼容笼呆,不考慮boxing與unboxing。
4旨别、vararg
private String print(Object... values) {
StringBuilder sb = new StringBuilder();
for (Object o : values) {
sb.append(o.toString())
.append(" ");
}
return sb.toString();
}
5诗赌、annotation
- Inherited表示該注解是否對類的子類繼承的方法等起作用
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InProgress { }
- 指定Target
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,
ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.ANNOTATION_TYPE})
public @interface TODO {
String value();
}
- Target類型
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE
}
- rentation表示annotation是否保留在編譯過的class文件中還是在運(yùn)行時可讀。
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
- 通過反射獲取元信息
public class ReflectionTester {
public ReflectionTester() {
}
public void testAnnotationPresent(PrintStream out) throws IOException {
Class c = Super.class;
boolean inProgress = c.isAnnotationPresent(InProgress.class);
if (inProgress) {
out.println("Super is In Progress");
} else {
out.println("Super is not In Progress");
}
}
public void testInheritedAnnotation(PrintStream out) throws IOException {
Class c = Sub.class;
boolean inProgress = c.isAnnotationPresent(InProgress.class);
if (inProgress) {
out.println("Sub is In Progress");
} else {
out.println("Sub is not In Progress");
}
}
public void testGetAnnotation(PrintStream out)
throws IOException, NoSuchMethodException {
Class c = AnnotationTester.class;
AnnotatedElement element = c.getMethod("calculateInterest",
float.class, float.class);
GroupTODO groupTodo = element.getAnnotation(GroupTODO.class);
String assignedTo = groupTodo.assignedTo();
out.println("TODO Item on Annotation Tester is assigned to: '" +
assignedTo + "'");
}
public void printAnnotations(AnnotatedElement e, PrintStream out)
throws IOException {
out.printf("Printing annotations for '%s'%n%n", e.toString());
Annotation[] annotations = e.getAnnotations();
for (Annotation a : annotations) {
out.printf(" * Annotation '%s' found%n",
a.annotationType().getName());
}
}
public static void main(String[] args) {
try {
ReflectionTester tester = new ReflectionTester();
tester.testAnnotationPresent(System.out);
tester.testInheritedAnnotation(System.out);
tester.testGetAnnotation(System.out);
Class c = AnnotationTester.class;
AnnotatedElement element = c.getMethod("calculateInterest",
float.class, float.class);
tester.printAnnotations(element, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6昼榛、for/in
for/in循環(huán)辦不到的事情:
(1)遍歷同時獲取index
(2)集合逗號拼接時去掉最后一個
(3)遍歷的同時刪除元素
7境肾、靜態(tài)import
import static java.lang.System.err;
import static java.lang.System.out;
import java.io.IOException;
import java.io.PrintStream;
public class StaticImporter {
public static void writeError(PrintStream err, String msg)
throws IOException {
// Note that err in the parameter list overshadows the imported err
err.println(msg);
}
public static void main(String[] args) {
if (args.length < 2) {
err.println(
"Incorrect usage: java com.oreilly.tiger.ch08 [arg1] [arg2]");
return;
}
out.println("Good morning, " + args[0]);
out.println("Have a " + args[1] + " day!");
try {
writeError(System.out, "Error occurred.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
8、格式化
/**
* java.text.DateFormat
* java.text.SimpleDateFormat
* java.text.MessageFormat
* java.text.NumberFormat
* java.text.ChoiceFormat
* java.text.DecimalFormat
*/
public class FormatTester {
public static void printf() {
//printf
String filename = "this is a file";
try {
File file = new File(filename);
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String line;
int i = 1;
while ((line = reader.readLine()) != null) {
System.out.printf("Line %d: %s%n", i++, line);
}
} catch (Exception e) {
System.err.printf("Unable to open file named '%s': %s",
filename, e.getMessage());
}
}
public static void stringFormat() {
// Format a string containing a date.
Calendar c = new GregorianCalendar(1995, MAY, 23);
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
// -> s == "Duke's Birthday: May 23, 1995"
System.out.println(s);
}
public static void formatter() {
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Explicit argument indices may be used to re-order output.
formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d");
// -> " d c b a"
// Optional locale as the first argument can be used to get
// locale-specific formatting of numbers. The precision and width can be
// given to round and align the value.
formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
// -> "e = +2,7183"
// The '(' numeric flag may be used to format negative numbers with
// parentheses rather than a minus sign. Group separators are
// automatically inserted.
formatter.format("Amount gained or lost since last statement: $ %(,.2f",
6217.58);
// -> "Amount gained or lost since last statement: $ (6,217.58)"
}
public static void messageFormat() {
String msg = "歡迎光臨胆屿,當(dāng)前({0})等待的業(yè)務(wù)受理的顧客有{1}位,請排號辦理業(yè)務(wù)偶宫!";
MessageFormat mf = new MessageFormat(msg);
String fmsg = mf.format(new Object[]{new Date(), 35});
System.out.println(fmsg);
}
public static void dateFormat(){
String str = "2010-1-10 17:39:21";
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
try {
System.out.println(format.format(format.parse(str)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
formatter();
stringFormat();
messageFormat();
dateFormat();
printf();
}
}
9非迹、threading
- uncaught exception
public class BubbleSortThread extends Thread {
private int[] numbers;
public BubbleSortThread(int[] numbers) {
setName("Simple Thread");
setUncaughtExceptionHandler(
new SimpleThreadExceptionHandler());
this.numbers = numbers;
}
public void run() {
int index = numbers.length;
boolean finished = false;
while (!finished) {
index--;
finished = true;
for (int i=0; i<index; i++) {
// Create error condition
if (numbers[i+1] < 0) {
throw new IllegalArgumentException(
"Cannot pass negative numbers into this thread!");
}
if (numbers[i] > numbers[i+1]) {
// swap
int temp = numbers[i];
numbers[i] = numbers[i+1];
numbers[i+1] = temp;
finished = false;
}
}
}
}
}
class SimpleThreadExceptionHandler implements
Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.err.printf("%s: %s at line %d of %s%n",
t.getName(),
e.toString(),
e.getStackTrace()[0].getLineNumber(),
e.getStackTrace()[0].getFileName());
}
}
- blocking queue
public class Producer extends Thread {
private BlockingQueue q;
private PrintStream out;
public Producer(BlockingQueue q, PrintStream out) {
setName("Producer");
this.q = q;
this.out = out;
}
public void run() {
try {
while (true) {
q.put(produce());
}
} catch (InterruptedException e) {
out.printf("%s interrupted: %s", getName(), e.getMessage());
}
}
private String produce() {
while (true) {
double r = Math.random();
// Only goes forward 1/10 of the time
if ((r*100) < 10) {
String s = String.format("Inserted at %tc", new Date());
return s;
}
}
}
}
- 線程池
著名的JUC類庫。
- 每次提交任務(wù)時纯趋,如果線程數(shù)還沒達(dá)到coreSize就創(chuàng)建新線程并綁定該任務(wù)憎兽。 所以第coreSize次提交任務(wù)后線程總數(shù)必達(dá)到coreSize,不會重用之前的空閑線程吵冒。
- 線程數(shù)達(dá)到coreSize后纯命,新增的任務(wù)就放到工作隊列里,而線程池里的線程則努力的使用take()從工作隊列里拉活來干痹栖。
- 如果隊列是個有界隊列亿汞,又如果線程池里的線程不能及時將任務(wù)取走,工作隊列可能會滿掉揪阿,插入任務(wù)就會失敗疗我,此時線程池就會緊急的再創(chuàng)建新的臨時線程來補(bǔ)救咆畏。
- 臨時線程使用poll(keepAliveTime,timeUnit)來從工作隊列拉活吴裤,如果時候到了仍然兩手空空沒拉到活旧找,表明它太閑了,就會被解雇掉麦牺。
- 如果core線程數(shù)+臨時線程數(shù) >maxSize钮蛛,則不能再創(chuàng)建新的臨時線程了,轉(zhuǎn)頭執(zhí)行RejectExecutionHanlder剖膳。默認(rèn)的AbortPolicy拋RejectedExecutionException異常魏颓,其他選擇包括靜默放棄當(dāng)前任務(wù)(Discard),放棄工作隊列里最老的任務(wù)(DisacardOldest)潮秘,或由主線程來直接執(zhí)行(CallerRuns)琼开,或你自己發(fā)揮想象力寫的一個。
10枕荞、其他
- Arrays
Arrays.sort(myArray);
Arrays.toString(myArray)
Arrays.binarySearch(myArray, 98)
Arrays.deepToString(ticTacToe)
Arrays.deepEquals(ticTacToe, ticTacToe3)
- Queue
避開集合的add/remove操作柜候,使用offer、poll操作(不拋異常)
Queue q = new LinkedList(); 采用它來實(shí)現(xiàn)queue
- Override返回類型
支持協(xié)變返回
- 單線程StringBuilder
線程不安全躏精,在單線程下替換string buffer提高性能
- java.lang.instrument
參考
Java6新特性
- JSR223腳本引擎
- JSR199--Java Compiler API
- JSR269--Pluggable Annotation Processing API
- 支持JDBC4.0規(guī)范
- JAX-WS 2.0規(guī)范
1渣刷、JSR223腳本引擎
Scripting for the Java Platform
- 基本使用
public class BasicScripting {
public void greet() throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
//支持通過名稱、文件擴(kuò)展名矗烛、MIMEtype查找
ScriptEngine engine = manager.getEngineByName("JavaScript");
// engine = manager.getEngineByExtension("js");
// engine = manager.getEngineByMimeType("text/javascript");
if (engine == null) {
throw new RuntimeException("找不到JavaScript語言執(zhí)行引擎辅柴。");
}
engine.eval("println('Hello!');");
}
public static void main(String[] args) {
try {
new BasicScripting().greet();
} catch (ScriptException ex) {
Logger.getLogger(BasicScripting.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
- 綁定上下文
public class ScriptContextBindings extends JsScriptRunner {
public void scriptContextBindings() throws ScriptException {
ScriptEngine engine = getJavaScriptEngine();
ScriptContext context = engine.getContext();
Bindings bindings1 = engine.createBindings();
bindings1.put("name", "Alex");
context.setBindings(bindings1, ScriptContext.GLOBAL_SCOPE);
Bindings bindings2 = engine.createBindings();
bindings2.put("name", "Bob");
context.setBindings(bindings2, ScriptContext.ENGINE_SCOPE);
engine.eval("println(name);");
}
public void useScriptContextValues() throws ScriptException {
ScriptEngine engine = getJavaScriptEngine();
ScriptContext context = engine.getContext();
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("name", "Alex");
engine.eval("println(name);");
}
public void attributeInBindings() throws ScriptException {
ScriptEngine engine = getJavaScriptEngine();
ScriptContext context = engine.getContext();
context.setAttribute("name", "Alex", ScriptContext.GLOBAL_SCOPE);
engine.eval("println(name);");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws ScriptException {
ScriptContextBindings scb = new ScriptContextBindings();
scb.scriptContextBindings();
scb.useScriptContextValues();
scb.attributeInBindings();
}
}
2、JSR199--Java Compiler API
public class JavaCompilerAPICompiler {
public void compile(Path src, Path output) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(src.toFile());
Iterable<String> options = Arrays.asList("-d", output.toString());
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);
boolean result = task.call();
}
}
}
3瞭吃、JSR269--Pluggable Annotation Processing API
一部分是進(jìn)行注解處理的javax.annotation.processing碌嘀,另一部分是對程序的靜態(tài)結(jié)構(gòu)進(jìn)行建模的javax.lang.model
4、其他
- 支持JDBC4.0規(guī)范
- JAX-WS 2.0規(guī)范(
包括JAXB 2.0
) - 輕量級HttpServer
參考
- Java 6 Features and Enhancements
- Java Platform, Standard Edition Differences between 5.0 fcs and 6 Beta
Java7新特性
- suppress異常(新語法)
- 捕獲多個異常(新語法)
- try-with-resources(新語法)
- JSR341-Expression Language Specification(新規(guī)范)
- JSR203-More New I/O APIs for the Java Platform(新規(guī)范)
- JSR292與InvokeDynamic
- 支持JDBC4.1規(guī)范
- Path接口歪架、DirectoryStream股冗、Files、WatchService
- jcmd
- fork/join framework
- Java Mission Control
1和蚪、suppress異常(新語法)
/**
* 記錄異常止状,不被淹沒
* addSuppressed
*/
class ReadFile {
public void read(String filename) throws BaseException {
FileInputStream input = null;
IOException readException = null;
try {
input = new FileInputStream(filename);
} catch (IOException ex) {
readException = ex;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ex) {
if (readException == null) {
readException = ex;
}else{
//使用java7的
readException.addSuppressed(ex);
}
}
}
if (readException != null) {
throw new BaseException(readException);
}
}
}
}
2、捕獲多個異常(新語法
)
public void handle() {
ExceptionThrower thrower = new ExceptionThrower();
try {
thrower.manyExceptions();
} catch (ExceptionA | ExceptionB ab) {
System.out.println(ab.getClass());
} catch (ExceptionC c) {
}
}
3攒霹、try-with-resources(新語法
)
/**
* try-with-resource
* 不需要使用finally來保證打開的流被正確關(guān)閉
* 這是自動完成的怯疤。
* @created 2014-07-21
*/
public class ResourceBasicUsage {
public String readFile(String path) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(String.format("%n"));
}
return builder.toString();
}
}
}
實(shí)現(xiàn)AutoCloseable
/**
* @created 2014-07-21
*/
public class CustomResource implements AutoCloseable {
public void close() throws Exception {
System.out.println("進(jìn)行資源釋放。");
}
public void useCustomResource() throws Exception {
try (CustomResource resource = new CustomResource()) {
System.out.println("使用資源催束。");
}
}
public static void main(String[] args) {
try {
new CustomResource().useCustomResource();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
4集峦、JSR341-Expression Language Specification(新規(guī)范
)
public static void main(String[] args){
ELProcessor el = new ELProcessor();
assert (el.eval("Math.random()") instanceof Double);
}
5、JSR203-More New I/O APIs for the Java Platform(新規(guī)范
)
- bytebuffer
public class ByteBufferUsage {
public void useByteBuffer() {
ByteBuffer buffer = ByteBuffer.allocate(32);
buffer.put((byte)1);
buffer.put(new byte[3]);
buffer.putChar('A');
buffer.putFloat(0.0f);
buffer.putLong(10, 100L);
System.out.println(buffer.getChar(4));
System.out.println(buffer.remaining());
}
public void byteOrder() {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(1);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.getInt(0); //值為16777216
}
public void compact() {
ByteBuffer buffer = ByteBuffer.allocate(32);
buffer.put(new byte[16]);
buffer.flip();
buffer.getInt();
buffer.compact();
int pos = buffer.position();
}
public void viewBuffer() {
ByteBuffer buffer = ByteBuffer.allocate(32);
buffer.putInt(1);
IntBuffer intBuffer = buffer.asIntBuffer();
intBuffer.put(2);
int value = buffer.getInt(); //值為2
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ByteBufferUsage bbu = new ByteBufferUsage();
bbu.useByteBuffer();
bbu.byteOrder();
bbu.compact();
bbu.viewBuffer();
}
}
- filechannel
public class FileChannelUsage {
public void openAndWrite() throws IOException {
FileChannel channel = FileChannel.open(Paths.get("my.txt"), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
ByteBuffer buffer = ByteBuffer.allocate(64);
buffer.putChar('A').flip();
channel.write(buffer);
}
public void readWriteAbsolute() throws IOException {
FileChannel channel = FileChannel.open(Paths.get("absolute.txt"), StandardOpenOption.READ, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
ByteBuffer writeBuffer = ByteBuffer.allocate(4).putChar('A').putChar('B');
writeBuffer.flip();
channel.write(writeBuffer, 1024);
ByteBuffer readBuffer = ByteBuffer.allocate(2);
channel.read(readBuffer, 1026);
readBuffer.flip();
char result = readBuffer.getChar(); //值為'B'
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
FileChannelUsage fcu = new FileChannelUsage();
fcu.openAndWrite();
fcu.readWriteAbsolute();
}
}
6、JSR292與InvokeDynamic
JSR 292: Supporting Dynamically Typed Languages on the JavaTM Platform少梁,支持在JVM上運(yùn)行動態(tài)類型語言洛口。在字節(jié)碼層面支持了InvokeDynamic。
- 方法句柄MethodHandle
public class ThreadPoolManager {
private final ScheduledExecutorService stpe = Executors
.newScheduledThreadPool(2);
private final BlockingQueue<WorkUnit<String>> lbq;
public ThreadPoolManager(BlockingQueue<WorkUnit<String>> lbq_) {
lbq = lbq_;
}
public ScheduledFuture<?> run(QueueReaderTask msgReader) {
msgReader.setQueue(lbq);
return stpe.scheduleAtFixedRate(msgReader, 10, 10, TimeUnit.MILLISECONDS);
}
private void cancel(final ScheduledFuture<?> hndl) {
stpe.schedule(new Runnable() {
public void run() {
hndl.cancel(true);
}
}, 10, TimeUnit.MILLISECONDS);
}
/**
* 使用傳統(tǒng)的反射api
*/
public Method makeReflective() {
Method meth = null;
try {
Class<?>[] argTypes = new Class[]{ScheduledFuture.class};
meth = ThreadPoolManager.class.getDeclaredMethod("cancel", argTypes);
meth.setAccessible(true);
} catch (IllegalArgumentException | NoSuchMethodException
| SecurityException e) {
e.printStackTrace();
}
return meth;
}
/**
* 使用代理類
* @return
*/
public CancelProxy makeProxy() {
return new CancelProxy();
}
/**
* 使用Java7的新api凯沪,MethodHandle
* invoke virtual 動態(tài)綁定后調(diào)用 obj.xxx
* invoke special 靜態(tài)綁定后調(diào)用 super.xxx
* @return
*/
public MethodHandle makeMh() {
MethodHandle mh;
MethodType desc = MethodType.methodType(void.class, ScheduledFuture.class);
try {
mh = MethodHandles.lookup().findVirtual(ThreadPoolManager.class,
"cancel", desc);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw (AssertionError) new AssertionError().initCause(e);
}
return mh;
}
public static class CancelProxy {
private CancelProxy() {
}
public void invoke(ThreadPoolManager mae_, ScheduledFuture<?> hndl_) {
mae_.cancel(hndl_);
}
}
}
- 調(diào)用
public class ThreadPoolMain {
/**
* 如果被繼承第焰,還能在靜態(tài)上下文尋找正確的class
*/
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private ThreadPoolManager manager;
public static void main(String[] args) {
ThreadPoolMain main = new ThreadPoolMain();
main.run();
}
private void cancelUsingReflection(ScheduledFuture<?> hndl) {
Method meth = manager.makeReflective();
try {
System.out.println("With Reflection");
meth.invoke(hndl);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
private void cancelUsingProxy(ScheduledFuture<?> hndl) {
CancelProxy proxy = manager.makeProxy();
System.out.println("With Proxy");
proxy.invoke(manager, hndl);
}
private void cancelUsingMH(ScheduledFuture<?> hndl) {
MethodHandle mh = manager.makeMh();
try {
System.out.println("With Method Handle");
mh.invokeExact(manager, hndl);
} catch (Throwable e) {
e.printStackTrace();
}
}
private void run() {
BlockingQueue<WorkUnit<String>> lbq = new LinkedBlockingQueue<>();
manager = new ThreadPoolManager(lbq);
final QueueReaderTask msgReader = new QueueReaderTask(100) {
@Override
public void doAction(String msg_) {
if (msg_ != null)
System.out.println("Msg recvd: " + msg_);
}
};
ScheduledFuture<?> hndl = manager.run(msgReader);
cancelUsingMH(hndl);
// cancelUsingProxy(hndl);
// cancelUsingReflection(hndl);
}
}
7、支持JDBC4.1規(guī)范
- abort方法
public class AbortConnection {
public void abortConnection() throws SQLException {
Connection connection = DriverManager
.getConnection("jdbc:derby://localhost/java7book");
ThreadPoolExecutor executor = new DebugExecutorService(2, 10, 60,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
connection.abort(executor);
executor.shutdown();
try {
executor.awaitTermination(5, TimeUnit.MINUTES);
System.out.println(executor.getCompletedTaskCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static class DebugExecutorService extends ThreadPoolExecutor {
public DebugExecutorService(int corePoolSize, int maximumPoolSize,
long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public void beforeExecute(Thread t, Runnable r) {
System.out.println("清理任務(wù):" + r.getClass());
super.beforeExecute(t, r);
}
}
public static void main(String[] args) {
AbortConnection ca = new AbortConnection();
try {
ca.abortConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 自動關(guān)閉
public class SetSchema {
public void setSchema() throws SQLException {
try (Connection connection = DriverManager
.getConnection("jdbc:derby://localhost/java7book")) {
connection.setSchema("DEMO_SCHEMA");
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM author")) {
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
}
}
public static void main(String[] args) {
SetSchema ss = new SetSchema();
try {
ss.setSchema();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 自動映射
public class UseSQLData {
public void useSQLData() throws SQLException {
try (Connection connection = DriverManager
.getConnection("jdbc:derby://localhost/java7book")) {
Map<String,Class<?>> typeMap = new HashMap<String,Class<?>>();
typeMap.put("java7book.Book", Book.class);
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM book")) {
while (rs.next()) {
System.out.println(rs.getObject(1, Book.class));
}
}
}
}
public static void main(String[] args) {
UseSQLData usd = new UseSQLData();
try {
usd.useSQLData();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
8妨马、Path接口(重要接口更新
)
public class PathUsage {
public void usePath() {
Path path1 = Paths.get("folder1", "sub1");
Path path2 = Paths.get("folder2", "sub2");
path1.resolve(path2); //folder1\sub1\folder2\sub2
path1.resolveSibling(path2); //folder1\folder2\sub2
path1.relativize(path2); //..\..\folder2\sub2
path1.subpath(0, 1); //folder1
path1.startsWith(path2); //false
path1.endsWith(path2); //false
Paths.get("folder1/./../folder2/my.text").normalize(); //folder2\my.text
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PathUsage usage = new PathUsage();
usage.usePath();
}
}
9挺举、DirectoryStream
public class ListFile {
public void listFiles() throws IOException {
Path path = Paths.get("");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.*")) {
for (Path entry: stream) {
//使用entry
System.out.println(entry);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
ListFile listFile = new ListFile();
listFile.listFiles();
}
}
10、Files
public class FilesUtils {
public void manipulateFiles() throws IOException {
Path newFile = Files.createFile(Paths.get("new.txt").toAbsolutePath());
List<String> content = new ArrayList<String>();
content.add("Hello");
content.add("World");
Files.write(newFile, content, Charset.forName("UTF-8"));
Files.size(newFile);
byte[] bytes = Files.readAllBytes(newFile);
ByteArrayOutputStream output = new ByteArrayOutputStream();
Files.copy(newFile, output);
Files.delete(newFile);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
FilesUtils fu = new FilesUtils();
fu.manipulateFiles();
}
}
11烘跺、WatchService
public class WatchAndCalculate {
public void calculate() throws IOException, InterruptedException {
WatchService service = FileSystems.getDefault().newWatchService();
Path path = Paths.get("").toAbsolutePath();
path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
while (true) {
WatchKey key = service.take();
for (WatchEvent<?> event : key.pollEvents()) {
Path createdPath = (Path) event.context();
createdPath = path.resolve(createdPath);
long size = Files.size(createdPath);
System.out.println(createdPath + " ==> " + size);
}
key.reset();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Throwable {
WatchAndCalculate wc = new WatchAndCalculate();
wc.calculate();
}
}
12湘纵、jcmd utility
jcmd是為了替代jps出現(xiàn)了,包含了jps的大部分功能并新增了一些新的功能滤淳。
- jcmd -l
列出所有的Java虛擬機(jī)梧喷,針對每一個虛擬機(jī)可以使用help列出它們支持的命令。
jcmd -l
15308 org.eclipse.jetty.xml.XmlConfiguration /tmp/start4070493346048555702.properties /opt/educat/apps/conf/jetty8.xml
5624 sun.tools.jcmd.JCmd -l
20967 org.apache.flume.node.Application --no-reload-conf -f /opt/educat/flume_agent/conf/flume.conf -n log_agent
- jcmd pid help
jcmd 15308 help
15308:
The following commands are available:
VM.commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Thread.print
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help
For more information about a specific command use 'help <command>'.
- jcmd pid VM.flags查看啟動參數(shù)
jcmd 15308 VM.flags
15308:
-XX:+DisableExplicitGC
-XX:ErrorFile=/var/codecraft/logs/catapp.vmerr.log.201505071655
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/codecraft/logs/catapp.heaperr.log.201505071655 -XX:InitialHeapSize=5368709120
-XX:+ManagementServer
-XX:MaxGCPauseMillis=100
-XX:MaxHeapSize=5368709120
-XX:MaxPermSize=268435456
-XX:+PrintAdaptiveSizePolicy
-XX:+PrintCommandLineFlags
-XX:+PrintGC
-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCDateStamps
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+PrintHeapAtGC
-XX:+PrintTenuringDistribution
-XX:StringTableSize=49999
-XX:+UnlockExperimentalVMOptions
-XX:+UseCompressedOops
-XX:+UseG1GC
- jcmd pid GC.heap_dump D:d.dump 導(dǎo)出堆信息
- jcmd pid GC.class_histogram查看系統(tǒng)中類的統(tǒng)計信息
- jcmd pid VM.system_properties查看系統(tǒng)屬性內(nèi)容
- jcmd pid Thread.print 打印線程棧
- jcmd pid VM.uptime 查看虛擬機(jī)啟動時間
- jcmd pid PerfCounter.print 查看性能統(tǒng)計
jcmd 15308 PerfCounter.print
15308:
java.ci.totalTime=79326405
java.cls.loadedClasses=19977
java.cls.sharedLoadedClasses=0
java.cls.sharedUnloadedClasses=0
java.cls.unloadedClasses=1443
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"
java.property.java.endorsed.dirs="/usr/local/jdk1.7.0_21/jre/lib/endorsed"
java.property.java.ext.dirs="/usr/local/jdk1.7.0_21/jre/lib/ext:/usr/java/packages/lib/ext"
java.property.java.home="/usr/local/jdk1.7.0_21/jre"
java.property.java.library.path="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib"
java.property.java.version="1.7.0_21"
java.property.java.vm.info="mixed mode"
java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM"
java.property.java.vm.specification.name="Java Virtual Machine Specification"
java.property.java.vm.specification.vendor="Oracle Corporation"
java.property.java.vm.specification.version="1.7"
java.property.java.vm.vendor="Oracle Corporation"
java.property.java.vm.version="23.21-b01"
java.rt.vmArgs="-javaagent:/opt/educat/apps/lib/jolokia-jvm-1.1.0-agent.jar=port=23061 -Xloggc:/var/codecraft/logs/catapp.gc.log.201505071655 -XX:ErrorFile=/var/codecraft/logs/catapp.vmerr.log.201505071655 -XX:HeapDumpPath=/var/codecraft/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/codecraft/logs -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -Dapp.ip=10.64.28.207 -Dapp.cont"
java.rt.vmFlags=""
java.threads.daemon=72
java.threads.live=128
java.threads.livePeak=129
java.threads.started=1444
sun.ci.compilerThread.0.compiles=2595
sun.ci.compilerThread.0.method=""
sun.ci.compilerThread.0.time=1290
sun.ci.compilerThread.0.type=1
sun.ci.compilerThread.1.compiles=2802
sun.ci.compilerThread.1.method=""
sun.ci.compilerThread.1.time=1413
sun.ci.compilerThread.1.type=2
sun.ci.lastFailedMethod=""
sun.ci.lastFailedType=0
sun.ci.lastInvalidatedMethod=""
sun.ci.lastInvalidatedType=0
sun.ci.lastMethod="org/codehaus/groovy/classgen/VariableScopeVisitor checkVariableNameForDeclaration"
sun.ci.lastSize=2184
sun.ci.lastType=1
sun.ci.nmethodCodeSize=12188576
sun.ci.nmethodSize=24492688
sun.ci.osrBytes=196694
sun.ci.osrCompiles=156
sun.ci.osrTime=8521713
sun.ci.standardBytes=2072839
sun.ci.standardCompiles=5241
sun.ci.standardTime=70804692
sun.ci.threads=2
sun.ci.totalBailouts=0
sun.ci.totalCompiles=5397
sun.ci.totalInvalidates=0
sun.classloader.findClassTime=358334873
sun.classloader.findClasses=507
sun.classloader.parentDelegationTime=30062667
sun.cls.appClassBytes=63743816
sun.cls.appClassLoadCount=58098
sun.cls.appClassLoadTime=9843833
sun.cls.appClassLoadTime.self=5288490
sun.cls.classInitTime=2617049
sun.cls.classInitTime.self=1088905
sun.cls.classLinkedTime=4605704
sun.cls.classLinkedTime.self=541928
sun.cls.classVerifyTime=4055324
sun.cls.classVerifyTime.self=2423448
sun.cls.defineAppClassTime=3206202
sun.cls.defineAppClassTime.self=386302
sun.cls.defineAppClasses=16465
sun.cls.initializedClasses=14546
sun.cls.isUnsyncloadClassSet=0
sun.cls.jniDefineClassNoLockCalls=94
sun.cls.jvmDefineClassNoLockCalls=4405
sun.cls.jvmFindLoadedClassNoLockCalls=32671
sun.cls.linkedClasses=16465
sun.cls.loadInstanceClassFailRate=0
sun.cls.loadedBytes=43314456
sun.cls.lookupSysClassTime=87247
sun.cls.methodBytes=34262690
sun.cls.nonSystemLoaderLockContentionRate=133
sun.cls.parseClassTime=3099390
sun.cls.parseClassTime.self=2670584
sun.cls.sharedClassLoadTime=9647
sun.cls.sharedLoadedBytes=0
sun.cls.sharedUnloadedBytes=0
sun.cls.sysClassBytes=12986737
sun.cls.sysClassLoadTime=503885
sun.cls.systemLoaderLockContentionRate=0
sun.cls.time=15382336
sun.cls.unloadedBytes=5087120
sun.cls.unsafeDefineClassCalls=1555
sun.cls.verifiedClasses=16383
sun.gc.cause="No GC"
sun.gc.collector.0.invocations=85
sun.gc.collector.0.lastEntryTime=24164511065
sun.gc.collector.0.lastExitTime=24164628388
sun.gc.collector.0.name="G1 incremental collections"
sun.gc.collector.0.time=7628099
sun.gc.collector.1.invocations=1
sun.gc.collector.1.lastEntryTime=24543200515
sun.gc.collector.1.lastExitTime=24544107869
sun.gc.collector.1.name="G1 stop-the-world full collections"
sun.gc.collector.1.time=907355
sun.gc.generation.0.agetable.bytes.00=0
sun.gc.generation.0.agetable.bytes.01=4294976
sun.gc.generation.0.agetable.bytes.02=2014880
sun.gc.generation.0.agetable.bytes.03=5406352
sun.gc.generation.0.agetable.bytes.04=4875176
sun.gc.generation.0.agetable.bytes.05=2865952
sun.gc.generation.0.agetable.bytes.06=4374048
sun.gc.generation.0.agetable.bytes.07=2058664
sun.gc.generation.0.agetable.bytes.08=3574376
sun.gc.generation.0.agetable.bytes.09=6923448
sun.gc.generation.0.agetable.bytes.10=1541088
sun.gc.generation.0.agetable.bytes.11=1347376
sun.gc.generation.0.agetable.bytes.12=735888
sun.gc.generation.0.agetable.bytes.13=402632
sun.gc.generation.0.agetable.bytes.14=713272
sun.gc.generation.0.agetable.bytes.15=728688
sun.gc.generation.0.agetable.size=16
sun.gc.generation.0.capacity=4510973976
sun.gc.generation.0.maxCapacity=5368709144
sun.gc.generation.0.minCapacity=24
sun.gc.generation.0.name="young"
sun.gc.generation.0.space.0.capacity=4510973960
sun.gc.generation.0.space.0.initCapacity=1128267784
sun.gc.generation.0.space.0.maxCapacity=5368709128
sun.gc.generation.0.space.0.name="eden"
sun.gc.generation.0.space.0.used=580911104
sun.gc.generation.0.space.1.capacity=8
sun.gc.generation.0.space.1.initCapacity=8
sun.gc.generation.0.space.1.maxCapacity=8
sun.gc.generation.0.space.1.name="s0"
sun.gc.generation.0.space.1.used=0
sun.gc.generation.0.space.2.capacity=8
sun.gc.generation.0.space.2.initCapacity=8
sun.gc.generation.0.space.2.maxCapacity=5368709128
sun.gc.generation.0.space.2.name="s1"
sun.gc.generation.0.space.2.used=0
sun.gc.generation.0.spaces=3
sun.gc.generation.1.capacity=857735176
sun.gc.generation.1.maxCapacity=5368709128
sun.gc.generation.1.minCapacity=8
sun.gc.generation.1.name="old"
sun.gc.generation.1.space.0.capacity=857735176
sun.gc.generation.1.space.0.initCapacity=4240441352
sun.gc.generation.1.space.0.maxCapacity=5368709128
sun.gc.generation.1.space.0.name="space"
sun.gc.generation.1.space.0.used=155730608
sun.gc.generation.1.spaces=1
sun.gc.generation.2.capacity=138412032
sun.gc.generation.2.maxCapacity=268435456
sun.gc.generation.2.minCapacity=20971520
sun.gc.generation.2.name="perm"
sun.gc.generation.2.space.0.capacity=138412032
sun.gc.generation.2.space.0.initCapacity=20971520
sun.gc.generation.2.space.0.maxCapacity=268435456
sun.gc.generation.2.space.0.name="perm"
sun.gc.generation.2.space.0.used=138212560
sun.gc.generation.2.spaces=1
sun.gc.lastCause="Heap Inspection Initiated GC"
sun.gc.policy.collectors=1
sun.gc.policy.desiredSurvivorSize=264241152
sun.gc.policy.generations=3
sun.gc.policy.maxTenuringThreshold=15
sun.gc.policy.name="GarbageFirst"
sun.gc.policy.tenuringThreshold=15
sun.gc.tlab.alloc=0
sun.gc.tlab.allocThreads=0
sun.gc.tlab.fastWaste=0
sun.gc.tlab.fills=0
sun.gc.tlab.gcWaste=0
sun.gc.tlab.maxFastWaste=0
sun.gc.tlab.maxFills=0
sun.gc.tlab.maxGcWaste=0
sun.gc.tlab.maxSlowAlloc=0
sun.gc.tlab.maxSlowWaste=0
sun.gc.tlab.slowAlloc=0
sun.gc.tlab.slowWaste=0
sun.management.JMXConnectorServer.0.authenticate="false"
sun.management.JMXConnectorServer.0.remoteAddress="service:jmx:rmi:///jndi/rmi://edu-cat02.lf.codecraft.com:8199/jmxrmi"
sun.management.JMXConnectorServer.0.ssl="false"
sun.management.JMXConnectorServer.0.sslNeedClientAuth="false"
sun.management.JMXConnectorServer.0.sslRegistry="false"
sun.management.JMXConnectorServer.address="service:jmx:rmi://127.0.0.1/stub/rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LnJlbW90ZS5ybWkuUk1JU2VydmVySW1wbF9TdHViAAAAAAAAAAICAAB4cgAaamF2YS5ybWkuc2VydmVyLlJlbW90ZVN0dWLp/tzJi+FlGgIAAHhyABxqYXZhLnJtaS5zZXJ2ZXIuUmVtb3RlT2JqZWN002G0kQxhMx4DAAB4cHc3AAtVbmljYXN0UmVmMgAADDEwLjY0LjI4LjIwNwAAhbjWmVwaDwiNg3l3YeUAAAFNLZX68oACAHg="
sun.os.hrt.frequency=1000000
sun.os.hrt.ticks=24580753795
sun.perfdata.majorVersion=2
sun.perfdata.minorVersion=0
sun.perfdata.overflow=0
sun.perfdata.size=32768
sun.perfdata.timestamp=259316
sun.perfdata.used=17792
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"
sun.property.sun.boot.library.path="/usr/local/jdk1.7.0_21/jre/lib/amd64"
sun.rt._sync_ContendedLockAttempts=297851
sun.rt._sync_Deflations=438863
sun.rt._sync_EmptyNotifications=0
sun.rt._sync_FailedSpins=0
sun.rt._sync_FutileWakeups=349651
sun.rt._sync_Inflations=438971
sun.rt._sync_MonExtant=16256
sun.rt._sync_MonInCirculation=0
sun.rt._sync_MonScavenged=0
sun.rt._sync_Notifications=1580811
sun.rt._sync_Parks=1935145
sun.rt._sync_PrivateA=0
sun.rt._sync_PrivateB=0
sun.rt._sync_SlowEnter=0
sun.rt._sync_SlowExit=0
sun.rt._sync_SlowNotify=0
sun.rt._sync_SlowNotifyAll=0
sun.rt._sync_SuccessfulSpins=0
sun.rt.applicationTime=24559855809
sun.rt.createVmBeginTime=1430988913170
sun.rt.createVmEndTime=1430988913429
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)"
sun.rt.interruptedBeforeIO=0
sun.rt.interruptedDuringIO=0
sun.rt.javaCommand="org.eclipse.jetty.xml.XmlConfiguration /tmp/start4070493346048555702.properties /opt/educat/apps/conf/jetty8.xml"
sun.rt.jvmCapabilities="1000000000000000000000000000000000000000000000000000000000000000"
sun.rt.jvmVersion=387252225
sun.rt.safepointSyncTime=2333795
sun.rt.safepointTime=15955181
sun.rt.safepoints=18365
sun.rt.threadInterruptSignaled=0
sun.rt.vmInitDoneTime=1430988913232
sun.threads.vmOperationTime=9516621
sun.urlClassLoader.readClassBytesTime=958824201
sun.zip.zipFile.openTime=72163038
sun.zip.zipFiles=3838
13脖咐、fork/join
Java7提供的一個用于并行執(zhí)行任務(wù)的框架铺敌,是一個把大任務(wù)分割成若干個小任務(wù),最終匯總每個小任務(wù)結(jié)果后得到大任務(wù)結(jié)果的框架屁擅。
14偿凭、Java Mission Control
在JDK7u40里頭提供了Java Mission Control,這個是從JRockit虛擬機(jī)里頭遷移過來的類似JVisualVm的東東派歌。
15弯囊、其他
- Binary Literals支持
- Numeric Literals的下劃線支持
- Strings in switch Statements
參考
文章太長了,此平臺不支持了胶果,另開一篇文章:Java8到Java12新特性見這里 http://www.reibang.com/p/e5fba5376371