// br automatically closed try // some code catch (SQLException | IOException e) // single block logger.log(e); throw new MyAppException(e);
1. Language Changes (Project Coin) Try-with-Resources (AutoCloseable) Automatically closes resources implementing AutoCloseable .
For parallel processing (divide and conquer). java 7
// Before: Map<String, List<String>> map = new HashMap<String, List<String>>(); Map<String, List<String>> map = new HashMap<>(); // Diamond Strings in switch String day = "MONDAY"; switch (day) case "MONDAY": System.out.println(1); break; case "TUESDAY": System.out.println(2); break; default: System.out.println(0);
WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir = Paths.get("/tmp"); dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); WatchKey key = watcher.take(); for (WatchEvent<?> event : key.pollEvents()) System.out.println("Event: " + event.kind() + " on " + event.context()); // br automatically closed try // some code
// Before Java 7 BufferedReader br = null; try br = new BufferedReader(new FileReader("file.txt")); br.readLine(); catch (IOException e) e.printStackTrace(); finally { if (br != null) try br.close(); catch (IOException e) {} } // Java 7 try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) br.readLine(); catch (IOException e) e.printStackTrace();
(file system events)
class MyRecursiveTask extends RecursiveTask<Long> private long workload; MyRecursiveTask(long w) workload = w; protected Long compute() if (workload < 2) return workload; MyRecursiveTask subtask = new MyRecursiveTask(workload - 1); subtask.fork(); return workload + subtask.join();