|
| 1 | +package lambdasinaction.chap11.standard; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import 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; |
| 10 | + |
| 11 | +public class StandardService { |
| 12 | + |
| 13 | + private final Executor executor = Executors.newFixedThreadPool(5, new ThreadFactory() { |
| 14 | + @Override |
| 15 | + public Thread newThread(Runnable r) { |
| 16 | + Thread t = new Thread(r); |
| 17 | + t.setDaemon(true); |
| 18 | + return t; |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + |
| 23 | + public String stadardCompany(String company) { |
| 24 | + delay(100); |
| 25 | + return "standard " + company; |
| 26 | + } |
| 27 | + |
| 28 | + public String standardSchool(String schoole) { |
| 29 | + delay(50); |
| 30 | + return "standard " + schoole; |
| 31 | + } |
| 32 | + |
| 33 | + public String standardSkill(String skill) { |
| 34 | + delay(200); |
| 35 | + return "standard " + skill; |
| 36 | + } |
| 37 | + |
| 38 | + public String standardTitle(String title) { |
| 39 | + delay(1000); |
| 40 | + return "standard " + title; |
| 41 | + } |
| 42 | + |
| 43 | + private void delay(int target) { |
| 44 | + int delayMils = new Random().nextInt(target); |
| 45 | + try { |
| 46 | + Thread.sleep(delayMils); |
| 47 | + } catch (InterruptedException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public String buildTag(String com, String school, String skill, String title) { |
| 53 | + return com + school + skill + title + "tag"; |
| 54 | + } |
| 55 | + |
| 56 | + public void oldFun(List<Resume> resumes) { |
| 57 | + for (Resume resume : resumes) { |
| 58 | + long start = System.nanoTime(); |
| 59 | + String company = this.stadardCompany(resume.getCompany()); |
| 60 | + String school= this.standardSchool(resume.getSchool()); |
| 61 | + String skill = this.standardSkill(resume.getSkill()); |
| 62 | + 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); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 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))); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + 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"); |
| 85 | + StandardService standardService = new StandardService(); |
| 86 | +// standardService.oldFun(new ArrayList<Resume>(){{add(resume)}}); |
| 87 | + } |
| 88 | +} |
0 commit comments