33import java .util .ArrayList ;
44import java .util .List ;
55import java .util .Random ;
6- import java .util .concurrent .CompletableFuture ;
7- import java .util .concurrent .Executor ;
8- import java .util .concurrent .Executors ;
9- import java .util .concurrent .ThreadFactory ;
6+ import java .util .concurrent .*;
7+ import java .util .function .Supplier ;
8+ import java .util .stream .Collectors ;
109
1110public class StandardService {
1211
@@ -19,7 +18,6 @@ public Thread newThread(Runnable r) {
1918 }
2019 });
2120
22-
2321 public String stadardCompany (String company ) {
2422 delay (100 );
2523 return "standard " + company ;
@@ -53,36 +51,69 @@ public String buildTag(String com, String school, String skill, String title) {
5351 return com + school + skill + title + "tag" ;
5452 }
5553
56- public void oldFun (List <Resume > resumes ) {
54+ public List <Resume > oldFun (List <Resume > resumes ) {
55+ List <Resume > result = new ArrayList <>();
5756 for (Resume resume : resumes ) {
58- long start = System .nanoTime ();
5957 String company = this .stadardCompany (resume .getCompany ());
6058 String school = this .standardSchool (resume .getSchool ());
6159 String skill = this .standardSkill (resume .getSkill ());
6260 String title = this .standardTitle (resume .getTitle ());
63- System .out .println (this .buildTag (company , school , skill , title ));
64- long retrivalTime = ((System .nanoTime () - start ) / 1_000_000 );
65- System .out .println (retrivalTime );
61+ result .add (new Resume (company , school , skill , title ));
6662 }
63+ System .out .println (result );
64+ return result ;
6765 }
6866
69- public void newFun (List <Resume > resumes ) {
70- // resumes.stream()
71- // .map(resume -> CompletableFuture.supplyAsync(() -> stadardCompany(resume.getCompany()), executor))
72- // .map(task -> task
73- // .thenCompose(resume -> CompletableFuture.supplyAsync(()->standardSchool(resume.), executor)))
74- // .map(task -> task
75- // .thenCompose(resume -> CompletableFuture.supplyAsync(()->standardSchool(resume.gets), executor)));
67+ // public List<Resume> futureFun(List<Resume> resumes) {
68+ // List<Future>
69+ // }
70+
71+ public List <Resume > newFun (List <Resume > resumes ) {
72+ List <CompletableFuture <Resume >> completableFutures = resumes .stream ()
73+ .map (resume -> CompletableFuture .supplyAsync (resume ::stadardCompany , executor ))
74+ .map (task -> task
75+ .thenCompose (resume -> CompletableFuture .supplyAsync (resume ::standardSchool , executor )))
76+ .map (task -> task
77+ .thenCompose (resume -> CompletableFuture .supplyAsync (resume ::standardSkill , executor )))
78+ .map (task -> task
79+ .thenCompose (resume -> CompletableFuture .supplyAsync (resume ::standardTitle , executor )))
80+ .collect (Collectors .toList ());
7681
82+ List <Resume > collect = completableFutures .stream ().map (CompletableFuture ::join ).collect (Collectors .toList ());
83+ System .out .println (collect );
84+ return collect ;
85+ }
86+
87+ public static void execute (String msg , Supplier <List <Resume >> supplier ) {
88+ long s = System .nanoTime ();
89+ supplier .get ();
90+ long e = System .nanoTime ();
91+ System .out .println (msg + " done in " + (e - s ) / 1_000_000 + " mesc" );
7792 }
7893
7994 public static void main (String [] args ) {
80- Resume resume = new Resume ();
81- resume .setCompany ("A" );
82- resume .setSchool ("B" );
83- resume .setSkill ("C" );
84- resume .setTitle ("D" );
95+ List <Resume > resumes = new ArrayList <Resume >(){{
96+ add (new Resume ("A1" , "B1" , "C1" , "D1" ));
97+ add (new Resume ("A2" , "B2" , "C2" , "D2" ));
98+ add (new Resume ("A3" , "B3" , "C3" , "D3" ));
99+ add (new Resume ("A4" , "B4" , "C4" , "D4" ));
100+ add (new Resume ("A5" , "B5" , "C5" , "D5" ));
101+ add (new Resume ("A6" , "B6" , "C6" , "D6" ));
102+ add (new Resume ("A7" , "B7" , "C7" , "D7" ));
103+ add (new Resume ("A8" , "B8" , "C8" , "D8" ));
104+ add (new Resume ("A9" , "B9" , "C9" , "D9" ));
105+ add (new Resume ("A0" , "B0" , "C0" , "D0" ));
106+ }};
107+
108+ List <Resume > resumes2 = new ArrayList <>();
109+
110+ for (int i = 0 ; i < 1000 ; i ++) {
111+ resumes2 .add (new Resume ("A" +i , "B0" , "C0" , "D0" ));
112+ }
113+
85114 StandardService standardService = new StandardService ();
86- // standardService.oldFun(new ArrayList<Resume>(){{add(resume)}});
115+ // execute("oldFun",()->standardService.oldFun(resumes) );
116+ execute ("newFun" ,()->standardService .newFun (resumes ) );
87117 }
118+
88119}
0 commit comments