New Feature of Java SE 7

31,572 views
31,620 views

Published on

This explain the new feature of Java SE 7.
Project Coin, NIO.2, Fork/Join, invokeDynamic and so on.

Published in: Technology, News & Politics
1 Comment
18 Likes
Statistics
Notes
No Downloads
Views
Total views
31,572
On SlideShare
0
From Embeds
0
Number of Embeds
19,246
Actions
Shares
0
Downloads
280
Comments
1
Likes
18
Embeds 0
No embeds

No notes for slide

New Feature of Java SE 7

  1. 1. Insert Picture Here>Java SE 7 Java Fusion Middleware Java http://yoshio3.com
  2. 2. Copyright© 2011, Oracle. All rights reserved.
  3. 3. Coin Java NIO.2 Da Vinci Machine Peoject Fork/Join Copyright© 2011, Oracle. All rights reserved.
  4. 4. Copyright© 2011, Oracle. All rights reserved.
  5. 5. 
                   Copyright© 2011, Oracle. All rights reserved.
  6. 6. Copyright© 2011, Oracle. All rights reserved.
  7. 7. "Java is the single most importantsoftware asset we have ever acquired." – Larry Ellison Copyright© 2011, Oracle. All rights reserved.
  8. 8. 1.0 1.2 5.0 1.1 1.3 1.41996 1997 1998 2000 2002 2004 2006 2011 2012 Copyright© 2011, Oracle. All rights reserved.
  9. 9. Copyright© 2011, Oracle. All rights reserved.
  10. 10. Project Coin (JSR 334) NIO.2 (JSR 203) Fork/Join Framework InvokeDynamic (JSR 292)•  •  •  •  •  •  •  •  •  •  •  •  •  •  Copyright© 2011, Oracle. All rights reserved.
  11. 11. Coin Copyright© 2011, Oracle. All rights reserved.
  12. 12. 
                       Copyright© 2011, Oracle. All rights reserved.
  13. 13. • switch String String s = “”; switch(s){ case “true”: doTrue(); break; case “false”: doFalse(); break; default: doDefault(); break; } Copyright© 2011, Oracle. All rights reserved.
  14. 14.       byte data = (byte)0b00100001             long creditCardNumber = 1234_5678_9012_3456L; int binryData = 0b1001_1001; Copyright© 2011, Oracle. All rights reserved.
  15. 15. 
   try{ … }catch(FileNotFoundException fnfe){ }catch(IOException ioe){ }   try{ … }catch(FileNotFoundException|IOException ex){ ex.printStackTrace(); } Copyright© 2011, Oracle. All rights reserved.
  16. 16. 
           try { … } catch (Throwable ex) { logger.log(ex); throw ex;//throw Throwable } Copyright© 2011, Oracle. All rights reserved.
  17. 17. 
         try { … } catch (final Throwable ex) { logger.log(ex); throw ex } Copyright© 2011, Oracle. All rights reserved.
  18. 18. 
   Map<String, String> map = new HashMap<String, String>(); Map<String, Map<Integer, String>> map = new HashMap<String, Map<Integer, String>>();  Map<String, String> map = new HashMap<>(); Map<String, Map<Integer, String>> map = new HashMap<>();      Copyright© 2011, Oracle. All rights reserved.
  19. 19.       private static void copyFile(File source, File target) { try (InputStream fin = new FileInputStream(source); OutputStream fout = new FileOutputStream(target)){ byte[] buf = new byte[8192]; int i; while ((i = fin.read(buf)) != -1) { fout.write(buf, 0, i); } }catch (Exception e) { e.printStackTrace(); } } Copyright© 2011, Oracle. All rights reserved.
  20. 20. Copyright© 2011, Oracle. All rights reserved.
  21. 21. 
                 Copyright© 2011, Oracle. All rights reserved.
  22. 22.   Copyright© 2011, Oracle. All rights reserved.
  23. 23. Java NIO.2 Java NIO.2 API API API I/O API Copyright© 2011, Oracle. All rights reserved.
  24. 24. 
                         Copyright© 2011, Oracle. All rights reserved.
  25. 25. 
 FileSystem fSystem = FileSystems.getDefault();! Path path1 = fSystem.getPath(/var/tmp/README.txt);! try(BufferedReader reader = ! Files.newBufferedReader(path1,! Charset.forName(UTF-8))) {! String line;! while ((line = reader.readLine()) != null)! System.out.println(line);! } catch (IOException ioe) {! ioe.printStackTrace();! } Copyright© 2011, Oracle. All rights reserved.
  26. 26. 
 Path path1 = Paths.get(/var,tmp,README.txt);! Path path2 = Paths.get(URI.create(file:///var/tmp/README.txt));! Path path3 = Paths.get(System.getProperty(user.home),README.txt); Copyright© 2011, Oracle. All rights reserved.
  27. 27. 
 Path path = Paths.get(/var,tmp,README.txt);!System.out.format(toString: %s%n, path.toString());!System.out.format(getFileName: %s%n, path.getFileName());!System.out.format(getName(0): %s%n, path.getName(0));!System.out.format(getNameCount: %d%n, path.getNameCount());!System.out.format(subpath(0,2): %s%n, path.subpath(0,2));!System.out.format(getParent: %s%n, path.getParent());!System.out.format(getRoot: %s%n, path.getRoot()); toString: /var/tmp/README.txt!getFileName: README.txt // !getName(0): var //Path 0 !getNameCount: 3 //Path !subpath(0,2): var/tmp // !getParent: /var/tmp // !getRoot: / // Copyright© 2011, Oracle. All rights reserved.
  28. 28. 
 Path path = Paths.get(URI.create(”! file:///var/test/../tmp/README.txt)); !Path norm = path.normalize();!System.out.format(path.toString: %s%n, ! path.toString());!System.out.format(norm.toString: %s%n, ! norm.toString()); path.toString: /var/test/../tmp/README.txt!norm.toString: /var/tmp/README.txt Copyright© 2011, Oracle. All rights reserved.
  29. 29. 
 Path path2 = Paths.get(../,var,test,README.txt); ! try{! Path real = path2.toRealPath(true);! System.out.format(real.toString: %s%n, real.toString());! }catch(java.nio.file.NoSuchFileException | IOException ioe){! ioe.printStackTrace();! }! real.toString: /var/test/README.txt Copyright© 2011, Oracle. All rights reserved.
  30. 30. 
 Path path = Paths.get(/var/tmp/foo);! Path path2 = Paths.get(/var/tmp/bar);!! Path fooToBar = path.relativize(path2);! System.out.format(fooToBar.toString: %s%n, ! fooToBar.toString()); fooToBar.toString: ../bar Copyright© 2011, Oracle. All rights reserved.
  31. 31. 
                 Copyright© 2011, Oracle. All rights reserved.
  32. 32. 
 public class CreateDirectory{! public static void main(String argv[]){! Path directory = Paths.get(/var/tmp/dir);! try{! Files.createDirectory(directory);! }catch(IOException ioe){! ioe.printStackTrace();! }! }!} Copyright© 2011, Oracle. All rights reserved.
  33. 33. 
 Path textFile = Paths.get(C:test.txt);! try(OutputStream out = Files.newOutputStream(textFile, ! StandardOpenOption.APPEND);! BufferedWriter bout = new BufferedWriter(! new OutputStreamWriter(out,UTF-8))){! bout.write(ADDITIONAL TEXTn);! }catch(IOException e){! System.err.println(e);! }! Copyright© 2011, Oracle. All rights reserved.
  34. 34. 
 try{! from = new FileInputStream(sourceFile);! to = new FileOutputStream(toFile);! byte[] buffer = new byte[4096];! int byteRead;! while((byteRead = from.read(buffer)) != -1)! to.write(buffer,0,byteRead);!} catch(IOException){} finally{! …! } FileSystem local = FileSystems.getDefault();! Path from = local.getPath(sourceFile);! Path to = local.getPath(toFile);! Files.copy(from, to, REPLACE_EXISTING); ! Copyright© 2011, Oracle. All rights reserved.
  35. 35. 
 Path target = Paths.get(/tmp/source.txt);!Path newLink = Paths.get(/tmp/symbolic.txt);!try{! Files.createSymbolicLink(newLink, target);!}catch(IOException | UnsupportedOperationException e){! System.err.println(e);!}! java.lang.UnsupportedOperationException:Symbolic links not supported on this operating system Copyright© 2011, Oracle. All rights reserved.
  36. 36. 
       finalFileSystem fs = FileSystems.getDefault();!Path startPath = fs.getPath(startDir);!SimpleFileVisitor visitor = new SimpleFileVisitorPath(){! @Override! public FileVisitResult visitFile(Path file, ! BasicFileAttributes attr) throws IOException{! PathMatcher matcher = fs.getPathMatcher(glob:*.txt);! if(matcher.matches(file.getFileName()))! System.out.println(file.toAbsolutePath());! return super.visitFile(file, attr);! }};!Files.walkFileTree(startPath, visitor);! Copyright© 2011, Oracle. All rights reserved.
  37. 37.                           Copyright© 2011, Oracle. All rights reserved.
  38. 38. FileSystem fs = FileSystems.getDefault();!WatchService ws = fs.newWatchService();! Path path = fs.getPath(C:tmp);! path.register(ws, StandardWatchEventKind.ENTRY_CREATE, ! StandardWatchEventKind.ENTRY_DELETE, ! StandardWatchEventKind.ENTRY_MODIFY);!for (;;) {! WatchKey key = ws.take();! ListWatchEvent? events = key.pollEvents();! for (WatchEvent obj : events) {! System.out.println(obj.kind().name()+ tFileName: ! + obj.context().toString());! }! key.reset();! }! Copyright© 2011, Oracle. All rights reserved.
  39. 39. 
       FileSystem fs = FileSystems.getDefault();! for(FileStore fstore: FileSystems.getDefault().getFileStores()){! long total = fstore.getTotalSpace() / 1024 ;! long used = (fstore.getTotalSpace() – ! fstore.getUnallocatedSpace()) / 1024 ;! long available = fstore.getUsableSpace() / 1024 ;! System.out.format(“%-15s %10d %14d %16d %n”,! fstore,total,used,available);! }! }! Copyright© 2011, Oracle. All rights reserved.
  40. 40. Da Vinci Machine ProjectJSR 292:Supporting Dynamically TypedLanguages on the JavaTM Platform Copyright© 2011, Oracle. All rights reserved.
  41. 41. Java Copyright© 2011, Oracle. All rights reserved.
  42. 42. Java Python Copyright© 2011, Oracle. All rights reserved.
  43. 43. Copyright© 2011, Oracle. All rights reserved.
  44. 44. Java 1997           Copyright© 2011, Oracle. All rights reserved.
  45. 45. Invokedynamic java.lang.invoke •  JavaVM• • •  Invokedyanamic •  Invokedynamic n •  Copyright© 2011, Oracle. All rights reserved.
  46. 46. 
 •  •  MethodHandle invokedyanamic •  Reflection     MethodType mt;! MethodHandle mh;! MethodHandles.Lookup lookup = MethodHandles.lookup();! mt = MethodType.methodType(String.class, char.class, ! char.class);! mh = lookup.findVirtual(String.class, replace, mt); ! String s = (String) mh.invokeExact(daddy,d,n); ! System.out.println(s);! Copyright© 2011, Oracle. All rights reserved.
  47. 47. Copyright© 2011, Oracle. All rights reserved.
  48. 48. 
             Copyright© 2011, Oracle. All rights reserved.
  49. 49. 
       Copyright© 2011, Oracle. All rights reserved.
  50. 50. 
 // !int threadNum = Runtime.getRuntime().availableProcessors();!//CPU !ExecutorService pool =! Executors.newFixedThreadPool(threadNum);! Copyright© 2011, Oracle. All rights reserved.
  51. 51. 
   Copyright© 2011, Oracle. All rights reserved.
  52. 52. 
 class MergeSort{ ! public int[] sort(int[] all){! if (all.length == 1) { ! return all;! } else { ! int[] left = new int[all.length/2]; ! System.arraycopy(all,0,left,0,left.length]; ! int[] right = new int[all.length – left.length];! System.arraycopy(all,left.length,right,! 0,right.length);! left = sort(left); ! right = sort(right); ! merge(left,right,all); ! return all;! }! }}! Copyright© 2011, Oracle. All rights reserved.
  53. 53. 
 public class MergeSort{ ! public int[] sort(int[] all){! int threads = Runtime.getRuntime().availableProcessors();! ForkJoinExecutor pool = new ForkJoinPool(threads); ! SortImpl sort = new SortImpl(all); ! pool.invoke(sort); ! return sort.result;! }!private class SortImpl extends RecursiveAction{ ! private int[] all; ! private int[] result;! SortImpl(int all){ ! this.all = all;! }! …! Copyright© 2011, Oracle. All rights reserved.
  54. 54. 
 protected void compute(){ ! if ((all.length 10 )) {! result = sequentialSort(all); ! } else {! int[] left = new int[all.length/2]; ! System.arraycopy(all,0,left,0,left.length); ! int[] right = new int[all.length – left.length];! System.arraycopy(all,left.length,right,0,right.length);! SortImpl task1 = new SortImp(left); ! SortImpl task2 = new SortImp(right); ! invokeAll(task1,task2); ! left = task1.result;! right = task2.result; ! merge(left,right,all); ! result = all;! }}! Copyright© 2011, Oracle. All rights reserved.
  55. 55. Copyright© 2011, Oracle. All rights reserved.
  56. 56.                         Copyright© 2011, Oracle. All rights reserved.
  57. 57.                         Copyright© 2011, Oracle. All rights reserved.
  58. 58.               Copyright© 2011, Oracle. All rights reserved.
  59. 59. Copyright© 2011, Oracle. All rights reserved.
  60. 60.                         Copyright© 2011, Oracle. All rights reserved.
  61. 61. Copyright© 2011, Oracle. All rights reserved.
  62. 62. 
                       Copyright© 2011, Oracle. All rights reserved.
  63. 63. 
                       Copyright© 2011, Oracle. All rights reserved.
  64. 64. Copyright© 2011, Oracle. All rights reserved.
  65. 65. Copyright© 2011, Oracle. All rights reserved.
  66. 66. Copyright© 2011, Oracle. All rights reserved.
  67. 67. Copyright© 2011, Oracle. All rights reserved.
  68. 68.             Copyright© 2011, Oracle. All rights reserved.
  69. 69.                 Copyright© 2011, Oracle. All rights reserved.
  70. 70. Copyright© 2011, Oracle. All rights reserved.
  71. 71. Copyright© 2011, Oracle. All rights reserved.

×