This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutionException; | |
import java.util.function.Predicate; | |
import java.util.stream.Collectors; | |
/** | |
* Created by sunels on 21.07.2016. | |
*/ | |
public class Test03toList { | |
public static void main (String[] args) { | |
Test03toList s = new Test03toList (); | |
List<CompletableFuture<String>> futures = new ArrayList<> (); | |
CompletableFuture<String> to = CompletableFuture.supplyAsync (s::findReceiver); | |
futures.add (to); | |
CompletableFuture<String> text = CompletableFuture.supplyAsync (s::createContent); | |
futures.add (text); | |
CompletableFuture<Void> allDoneFuture = CompletableFuture.allOf (futures.toArray (new CompletableFuture[futures.size ()])); | |
allDoneFuture.join (); | |
System.out.println ("to:" + getCompletionResultOf (to) + ", text:" + getCompletionResultOf (text)); | |
} | |
private static String getCompletionResultOf (CompletableFuture<String> to) { | |
String result = null; | |
try { | |
result = to.get (); | |
} catch (InterruptedException e) { | |
e.printStackTrace (); | |
} catch (ExecutionException e) { | |
e.printStackTrace (); | |
} | |
return result; | |
} | |
public String findReceiver () { | |
zleep (2000l); | |
return "054212312313"; | |
} | |
public String createContent () { | |
zleep (4000l); | |
return "Sn.serkan sunel faturani ödemedin"; | |
} | |
private void zleep (long dur) { | |
try { | |
Thread.currentThread ().sleep (dur); | |
} catch (InterruptedException e) { | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutionException; | |
import java.util.function.Predicate; | |
import java.util.stream.Collectors; | |
/** | |
* Created by sunels on 21.07.2016. | |
*/ | |
public class Test03toList { | |
public static void main (String[] args) { | |
Test03toList s = new Test03toList (); | |
List<CompletableFuture<String>> futures = new ArrayList<> (); | |
CompletableFuture<String> to = CompletableFuture.supplyAsync (s::findReceiver); | |
futures.add (to); | |
CompletableFuture<String> text = CompletableFuture.supplyAsync (s::createContent); | |
futures.add (text); | |
try { | |
long start1= System.currentTimeMillis (); | |
String result1 = to.get (); | |
System.out.println ("Duration -1:" + (System.currentTimeMillis () - start1)); | |
} catch (InterruptedException e) { | |
e.printStackTrace (); | |
} catch (ExecutionException e) { | |
e.printStackTrace (); | |
} | |
try { | |
long start2= System.currentTimeMillis (); | |
String result2 = text.get (); | |
System.out.println ("Duration -2:" + (System.currentTimeMillis () - start2)); | |
} catch (InterruptedException e) { | |
e.printStackTrace (); | |
} catch (ExecutionException e) { | |
e.printStackTrace (); | |
} | |
System.out.println ("-- !"); | |
} | |
private <U> U collectAllresponsesAndFinalizeThem (Void aVoid) { | |
System.out.println (" Things seems like to be done ! "); | |
return null; | |
} | |
private static String getCompletionResultOf (CompletableFuture<String> to) { | |
String result = null; | |
try { | |
result = to.get (); | |
} catch (InterruptedException e) { | |
e.printStackTrace (); | |
} catch (ExecutionException e) { | |
e.printStackTrace (); | |
} | |
return result; | |
} | |
public String findReceiver () { | |
zleep (14000l); | |
return "054212312313"; | |
} | |
public String createContent () { | |
zleep (24000l); | |
return "Sn.serkan sunel faturani ödemedin"; | |
} | |
private void zleep (long dur) { | |
try { | |
Thread.currentThread ().sleep (dur); | |
} catch (InterruptedException e) { | |
} | |
} | |
} |
No comments:
Post a Comment