![Page 1: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/1.jpg)
Java SE 7Fork / Join Framework
Language Evolutions (Project Coin)
Julien Ponge
![Page 2: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/2.jpg)
Fork / Join
![Page 3: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/3.jpg)
synchronized
wait()notify()< 5
java.lang.Threadjava.lang.Runnable
![Page 4: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/4.jpg)
Thread thread = new Thread() { public void run() { System.out.println(">>> Hello!"); }}; thread.start();thread.join();
![Page 5: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/5.jpg)
java.util.concurrent
executorsconcurrent queuesconcurrent collectionsatomic variablessynchronization patternsrich locks
5, 6
![Page 6: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/6.jpg)
class Sum implements Callable<Long> { private final long from; private final long to; Sum(long from, long to) { this.from = from; this.to = to; } public Long call() { long acc = 0; for (long i = from; i <= to; i++) { acc = acc + i; } return acc; } }
![Page 7: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/7.jpg)
ExecutorService executor = Executors.newFixedThreadPool(2);
List<Future<Long>> results = executor.invokeAll(asList( new Sum(0, 10), new Sum(100, 1000), new Sum(10000, 1000000)));
for (Future<Long> result : results) { System.out.println(result.get());}
![Page 8: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/8.jpg)
Threads made easy
Concurrency made easier
Parallelism made easier
1.01.11.21.31.4
56
7
![Page 9: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/9.jpg)
n1 n2 n3 n4 n5 n6 n8n7 n9 ... ... ......
sum1 sum2 sum3sum1 + sum2 sum3 + (...)
total sum
Sum of an array
![Page 10: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/10.jpg)
A need for divide and conquer
![Page 11: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/11.jpg)
SplitFork subtasksJoin subtasksCompose results
Thread managementMaximize parallelism
Work stealing
Your workForkJoinPool
![Page 12: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/12.jpg)
Child ForkJoinTask
fork() fork()join() join()
Child ForkJoinTask
ForkJoinTask
RecursiveActionvs
RecursiveTask
![Page 13: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/13.jpg)
Document wordcounting task
Document wordcounting task
Folder wordcounting task
Folder wordcounting task
Document wordcounting task
Document wordcounting task
3
n
2
51
10
16
fork()
join()
![Page 14: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/14.jpg)
class FolderSearchTask extends RecursiveTask<Long> {(...)
protected Long compute() { long count = 0L; List<RecursiveTask<Long>> forks = new LinkedList<>();
for (Folder subFolder : folder.getSubFolders()) { FolderSearchTask task = new FolderSearchTask(subFolder, searchedWord); forks.add(task); task.fork(); } for (Document document : folder.getDocuments()) { DocumentSearchTask task = new DocumentSearchTask(document, searchedWord); forks.add(task); task.fork(); }
for (RecursiveTask<Long> task : forks) { count = count + task.join(); } return count; }
(...)}
![Page 15: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/15.jpg)
class FolderSearchTask extends RecursiveTask<Long> {(...)
protected Long compute() { long count = 0L; List<RecursiveTask<Long>> forks = new LinkedList<>();
for (Folder subFolder : folder.getSubFolders()) { FolderSearchTask task = new FolderSearchTask(subFolder, searchedWord); forks.add(task); task.fork(); } for (Document document : folder.getDocuments()) { DocumentSearchTask task = new DocumentSearchTask(document, searchedWord); forks.add(task); task.fork(); }
for (RecursiveTask<Long> task : forks) { count = count + task.join(); } return count; }
(...)}
Split
![Page 16: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/16.jpg)
class FolderSearchTask extends RecursiveTask<Long> {(...)
protected Long compute() { long count = 0L; List<RecursiveTask<Long>> forks = new LinkedList<>();
for (Folder subFolder : folder.getSubFolders()) { FolderSearchTask task = new FolderSearchTask(subFolder, searchedWord); forks.add(task); task.fork(); } for (Document document : folder.getDocuments()) { DocumentSearchTask task = new DocumentSearchTask(document, searchedWord); forks.add(task); task.fork(); }
for (RecursiveTask<Long> task : forks) { count = count + task.join(); } return count; }
(...)}
Fork
![Page 17: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/17.jpg)
class FolderSearchTask extends RecursiveTask<Long> {(...)
protected Long compute() { long count = 0L; List<RecursiveTask<Long>> forks = new LinkedList<>();
for (Folder subFolder : folder.getSubFolders()) { FolderSearchTask task = new FolderSearchTask(subFolder, searchedWord); forks.add(task); task.fork(); } for (Document document : folder.getDocuments()) { DocumentSearchTask task = new DocumentSearchTask(document, searchedWord); forks.add(task); task.fork(); }
for (RecursiveTask<Long> task : forks) { count = count + task.join(); } return count; }
(...)}
Join, Compose
![Page 18: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/18.jpg)
(F/J demo)
![Page 19: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/19.jpg)
1"
2"
3"
4"
5"
6"
7"
2" 4" 6" 8" 10" 12"
Speedup&
#cores
![Page 20: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/20.jpg)
No I/ONo synchronization / locks
Decompose in simple recursive tasksDo not decompose below a threshold
Take advantage of multicores with no pain
You have more F/J candidate algorithms than you think!
![Page 21: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/21.jpg)
try-with-resources
![Page 22: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/22.jpg)
private void writeSomeData() throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream("data")); out.writeInt(666); out.writeUTF("Hello"); out.close();}
![Page 23: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/23.jpg)
private void writeSomeData() throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream("data")); out.writeInt(666); out.writeUTF("Hello"); out.close();}
what if...
![Page 24: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/24.jpg)
private void writeSomeData() throws IOException { DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream("data")); out.writeInt(666); out.writeUTF("Hello"); } finally { if (out != null) { out.close(); } } }
![Page 25: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/25.jpg)
private void writeSomeData() throws IOException { DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream("data")); out.writeInt(666); out.writeUTF("Hello"); } finally { if (out != null) { out.close(); } } }
...this is still far from correct!
![Page 26: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/26.jpg)
try (
FileOutputStream out = new FileOutputStream("output"); FileInputStream in1 = new FileInputStream(“input1”); FileInputStream in2 = new FileInputStream(“input2”)
) {
// Do something useful with those 3 streams! // out, in1 and in2 will be closed in any case
out.write(in1.read()); out.write(in2.read());}
![Page 27: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/27.jpg)
public class AutoClose implements AutoCloseable { @Override public void close() { System.out.println(">>> close()"); throw new RuntimeException("Exception in close()"); } public void work() throws MyException { System.out.println(">>> work()"); throw new MyException("Exception in work()"); }}
![Page 28: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/28.jpg)
AutoClose autoClose = new AutoClose();try { autoClose.work();} finally { autoClose.close();}
![Page 29: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/29.jpg)
AutoClose autoClose = new AutoClose();try { autoClose.work();} finally { autoClose.close();}
>>> work() >>> close() java.lang.RuntimeException: Exception in close() at AutoClose.close(AutoClose.java:6) at AutoClose.runWithMasking(AutoClose.java:19) at AutoClose.main(AutoClose.java:52)
![Page 30: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/30.jpg)
AutoClose autoClose = new AutoClose();try { autoClose.work();} finally { autoClose.close();}
>>> work() >>> close() java.lang.RuntimeException: Exception in close() at AutoClose.close(AutoClose.java:6) at AutoClose.runWithMasking(AutoClose.java:19) at AutoClose.main(AutoClose.java:52)
MyException masked by RuntimeException
![Page 31: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/31.jpg)
“Caused by” ≠ “Also happened”
![Page 32: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/32.jpg)
AutoClose autoClose = new AutoClose();MyException myException = null;try { autoClose.work();} catch (MyException e) { myException = e; throw e;} finally { if (myException != null) { try { autoClose.close(); } catch (Throwable t) { myException.addSuppressed(t); } } else { autoClose.close(); }}
![Page 33: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/33.jpg)
AutoClose autoClose = new AutoClose();MyException myException = null;try { autoClose.work();} catch (MyException e) { myException = e; throw e;} finally { if (myException != null) { try { autoClose.close(); } catch (Throwable t) { myException.addSuppressed(t); } } else { autoClose.close(); }}
![Page 34: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/34.jpg)
AutoClose autoClose = new AutoClose();MyException myException = null;try { autoClose.work();} catch (MyException e) { myException = e; throw e;} finally { if (myException != null) { try { autoClose.close(); } catch (Throwable t) { myException.addSuppressed(t); } } else { autoClose.close(); }}
![Page 35: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/35.jpg)
AutoClose autoClose = new AutoClose();MyException myException = null;try { autoClose.work();} catch (MyException e) { myException = e; throw e;} finally { if (myException != null) { try { autoClose.close(); } catch (Throwable t) { myException.addSuppressed(t); } } else { autoClose.close(); }}
![Page 36: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/36.jpg)
AutoClose autoClose = new AutoClose();MyException myException = null;try { autoClose.work();} catch (MyException e) { myException = e; throw e;} finally { if (myException != null) { try { autoClose.close(); } catch (Throwable t) { myException.addSuppressed(t); } } else { autoClose.close(); }}
![Page 37: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/37.jpg)
try (AutoClose autoClose = new AutoClose()) { autoClose.work();}
![Page 38: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/38.jpg)
try (AutoClose autoClose = new AutoClose()) { autoClose.work();}
>>> work() >>> close() MyException: Exception in work() at AutoClose.work(AutoClose.java:11) at AutoClose.main(AutoClose.java:16) Suppressed: java.lang.RuntimeException: Exception in close() at AutoClose.close(AutoClose.java:6) at AutoClose.main(AutoClose.java:17)
![Page 39: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/39.jpg)
public void compress(String input, String output) throws IOException { try( FileInputStream fin = new FileInputStream(input); FileOutputStream fout = new FileOutputStream(output); GZIPOutputStream out = new GZIPOutputStream(fout) ) { byte[] buffer = new byte[4096]; int nread = 0; while ((nread = fin.read(buffer)) != -1) { out.write(buffer, 0, nread); } }}
![Page 40: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/40.jpg)
public void compress(String paramString1, String paramString2) throws IOException { FileInputStream localFileInputStream = new FileInputStream(paramString1); Object localObject1 = null; try { FileOutputStream localFileOutputStream = new FileOutputStream(paramString2); Object localObject2 = null; try { GZIPOutputStream localGZIPOutputStream = new GZIPOutputStream(localFileOutputStream); Object localObject3 = null; try { byte[] arrayOfByte = new byte[4096]; int i = 0; while ((i = localFileInputStream.read(arrayOfByte)) != -1) { localGZIPOutputStream.write(arrayOfByte, 0, i); } } catch (Throwable localThrowable6) { localObject3 = localThrowable6; throw localThrowable6; } finally { if (localGZIPOutputStream != null) { if (localObject3 != null) { try { localGZIPOutputStream.close(); } catch (Throwable localThrowable7) { localObject3.addSuppressed(localThrowable7); } } else { localGZIPOutputStream.close(); } } } } catch (Throwable localThrowable4) { localObject2 = localThrowable4; throw localThrowable4; } finally { if (localFileOutputStream != null) { if (localObject2 != null) { try { localFileOutputStream.close(); } catch (Throwable localThrowable8) { localObject2.addSuppressed(localThrowable8); } } else { localFileOutputStream.close(); } } } } catch (Throwable localThrowable2) { localObject1 = localThrowable2; throw localThrowable2; } finally { if (localFileInputStream != null) { if (localObject1 != null) { try { localFileInputStream.close(); } catch (Throwable localThrowable9) { localObject1.addSuppressed(localThrowable9); } } else { localFileInputStream.close(); } } } }
public void compress(String input, String output) throws IOException { try( FileInputStream fin = new FileInputStream(input); FileOutputStream fout = new FileOutputStream(output); GZIPOutputStream out = new GZIPOutputStream(fout) ) { byte[] buffer = new byte[4096]; int nread = 0; while ((nread = fin.read(buffer)) != -1) { out.write(buffer, 0, nread); } }}
![Page 41: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/41.jpg)
Not just syntactic sugarClutter-free, correct code
close(): - be more specific than java.lang.Exception - no exception if it can’t fail - no exception that shall not be suppressed (e.g., java.lang.InterruptedException)
![Page 42: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/42.jpg)
Diamond <>
![Page 43: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/43.jpg)
List<String> strings = new LinkedList<Integer>();
Map<String, List<String>> contacts = new HashMap<Integer, String>();
![Page 44: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/44.jpg)
List<String> strings = new LinkedList<String>();strings.add("hello");strings.add("world");
Map<String, List<String>> contacts = new HashMap<String, List<String>>();contacts.put("Julien", new LinkedList<String>());contacts.get("Julien").addAll(asList("Foo", "Bar", "Baz"));
![Page 45: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/45.jpg)
List<String> strings = new LinkedList<>();strings.add("hello");strings.add("world");
Map<String, List<String>> contacts = new HashMap<>();contacts.put("Julien", new LinkedList<String>());contacts.get("Julien").addAll(asList("Foo", "Bar", "Baz"));
![Page 46: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/46.jpg)
Map<String, String> map = new HashMap<String, String>() { { put("foo", "bar"); put("bar", "baz"); }};
![Page 47: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/47.jpg)
Map<String, String> map = new HashMap<>() { { put("foo", "bar"); put("bar", "baz"); }};
![Page 48: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/48.jpg)
Map<String, String> map = new HashMap<>() { { put("foo", "bar"); put("bar", "baz"); }};
Diamond.java:43: error: cannot infer type arguments for HashMap; Map<String, String> map = new HashMap<>() { ^ reason: cannot use '<>' with anonymous inner classes1 error
![Page 49: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/49.jpg)
class SomeClass<T extends Serializable & CharSequence> { }
Non-denotable type
![Page 50: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/50.jpg)
class SomeClass<T extends Serializable & CharSequence> { }
Non-denotable type
SomeClass<?> foo = new SomeClass<String>();SomeClass<?> fooInner = new SomeClass<String>() { };
SomeClass<?> bar = new SomeClass<>();
SomeClass<?> bar = new SomeClass<>() { };
![Page 51: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/51.jpg)
class SomeClass<T extends Serializable & CharSequence> { }
Non-denotable type
SomeClass<?> foo = new SomeClass<String>();SomeClass<?> fooInner = new SomeClass<String>() { };
SomeClass<?> bar = new SomeClass<>();
SomeClass<?> bar = new SomeClass<>() { };
No denotable typeto generate a class
![Page 52: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/52.jpg)
Less ceremony when using generics
No diamond with inner classes
![Page 53: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/53.jpg)
Simplified varargs
![Page 54: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/54.jpg)
private static <T> void doSomethingBad(List<T> list, T... values) { values[0] = list.get(0);}
public static void main(String[] args) { List list = Arrays.asList("foo", "bar", "baz"); doSomethingBad(list, 1, 2, 3);}
![Page 55: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/55.jpg)
private static <T> void doSomethingBad(List<T> list, T... values) { values[0] = list.get(0);}
public static void main(String[] args) { List list = Arrays.asList("foo", "bar", "baz"); doSomethingBad(list, 1, 2, 3);}
Heap Pollution: List = List<String>
This yields warnings + crash:
![Page 56: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/56.jpg)
private static <T> void doSomethingGood(List<T> list, T... values) { for (T value : values) { list.add(value); }}
![Page 57: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/57.jpg)
private static <T> void doSomethingGood(List<T> list, T... values) { for (T value : values) { list.add(value); }}
$ javac Good.java Note: Good.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details.
![Page 58: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/58.jpg)
@SuppressWarnings({“unchecked”,“varargs”})
public static void main(String[] args) { List<String> list = new LinkedList<>(); doSomethingGood(list, "hi", "there", "!");}
$ javac Good.java $
![Page 59: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/59.jpg)
@SafeVarargsprivate static <T> void doSomethingGood(List<T> list, T... values) { for (T value : values) { list.add(value); }}
static, final methods, constructors
![Page 60: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/60.jpg)
Mark your code as safe for varargs
You can’t cheat with @SafeVarags
Remove @SuppressWarnings in client code and pay attention to real warnings
![Page 61: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/61.jpg)
Multi-catch & more precise rethrow
![Page 62: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/62.jpg)
Class<?> stringClass = Class.forName("java.lang.String"); Object instance = stringClass.newInstance(); Method toStringMethod = stringClass.getMethod("toString"); System.out.println(toStringMethod.invoke(instance));
![Page 63: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/63.jpg)
try { Class<?> stringClass = Class.forName("java.lang.String"); Object instance = stringClass.newInstance(); Method toStringMethod = stringClass.getMethod("toString"); System.out.println(toStringMethod.invoke(instance));} catch (ClassNotFoundException e) { e.printStackTrace();} catch (InstantiationException e) { e.printStackTrace();} catch (IllegalAccessException e) { e.printStackTrace();} catch (NoSuchMethodException e) { e.printStackTrace();} catch (InvocationTargetException e) { e.printStackTrace();}
![Page 64: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/64.jpg)
try { Class<?> stringClass = Class.forName("java.lang.String"); Object instance = stringClass.newInstance(); Method toStringMethod = stringClass.getMethod("toString"); System.out.println(toStringMethod.invoke(instance));} catch (Throwable t) { t.printStackTrace();}
![Page 65: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/65.jpg)
try { Class<?> stringClass = Class.forName("java.lang.String"); Object instance = stringClass.newInstance(); Method toStringMethod = stringClass.getMethod("toString"); System.out.println(toStringMethod.invoke(instance));} catch (Throwable t) { t.printStackTrace();}
How about SecurityException?
![Page 66: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/66.jpg)
try { Class<?> stringClass = Class.forName("java.lang.String"); Object instance = stringClass.newInstance(); Method toStringMethod = stringClass.getMethod("toString"); System.out.println(toStringMethod.invoke(instance));} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace();}
Union of alternatives
![Page 67: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/67.jpg)
catch (SomeException e)
Now implicitly final unless assigned...
![Page 68: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/68.jpg)
static class SomeRootException extends Exception { }static class SomeChildException extends SomeRootException { }static class SomeOtherChildException extends SomeRootException { }
public static void main(String... args) throws Throwable {try { throw new SomeChildException();} catch (SomeRootException firstException) { try { throw firstException; } catch (SomeOtherChildException secondException) { System.out.println("I got you!"); }}
![Page 69: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/69.jpg)
static class SomeRootException extends Exception { }static class SomeChildException extends SomeRootException { }static class SomeOtherChildException extends SomeRootException { }
public static void main(String... args) throws Throwable {try { throw new SomeChildException();} catch (SomeRootException firstException) { try { throw firstException; } catch (SomeOtherChildException secondException) { System.out.println("I got you!"); }}
$ javac Imprecise.java Imprecise.java:13: error: exception SomeOtherChildException is never thrown in body of corresponding try statement } catch (SomeOtherChildException secondException) { ^1 error
![Page 70: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/70.jpg)
Less clutterBe preciseDo not capture unintended exceptions
Better exception flow analysis
![Page 71: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/71.jpg)
Minor additions
![Page 72: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/72.jpg)
// 123 in decimal, octal, hexadecimal and binarybyte decimal = 123;byte octal = 0_173;byte hexadecimal = 0x7b;byte binary = 0b0111_1011;
// Other valuesdouble doubleValue = 1.111_222_444F;long longValue = 1_234_567_898L;long longHexa = 0x1234_3b3b_0123_cdefL;
![Page 73: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/73.jpg)
public static boolean isTrue(String str) { switch(str.trim().toUpperCase()) { case "OK": case "YES": case "TRUE": return true;
case "KO": case "NO": case "FALSE": return false;
default: throw new IllegalArgumentException("Not a valid true/false string."); } }
![Page 74: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/74.jpg)
public static boolean isTrue(String s) { String str = s.trim().toUpperCase(); int jump = -1; switch(str.hashCode()) { case 2404: if (str.equals("KO")) { jump = 3; } break;(...) switch(jump) {(...) case 3: case 4: case 5: return false; default: throw new IllegalArgumentException( "Not a valid true/false string."); }}
Bucket
Real code
![Page 75: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/75.jpg)
Fork and Join: Java Can Excel at Painless Parallel Programming Too!
Better Resource Management withJava SE 7: Beyond Syntactic Sugar
Oracle Technology Network
http://goo.gl/tostz
http://goo.gl/7ybgr
(more soon...)
![Page 76: Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Project Coin](https://reader033.vdocument.in/reader033/viewer/2022060110/555c3e0bd8b42a2c068b4bbf/html5/thumbnails/76.jpg)
Julien Ponge
@jpongehttp://julien.ponge.info/