Skip to content

Commit 3026b1a

Browse files
committed
local
1 parent b21c2b1 commit 3026b1a

File tree

11 files changed

+237
-38
lines changed

11 files changed

+237
-38
lines changed

src/main/java/lambdasinaction/chap11/BestPriceFinder.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package lambdasinaction.chap11;
22

3+
import com.sun.xml.internal.ws.util.CompletedFuture;
4+
35
import java.util.Arrays;
46
import java.util.List;
57
import java.util.concurrent.CompletableFuture;
@@ -27,6 +29,12 @@ public Thread newThread(Runnable r) {
2729
});
2830

2931
public List<String> findPricesSequential(String product) {
32+
// shops.stream().map(shop -> shop.getPrice(product))
33+
// .map(Quote::parse)
34+
// .map(Discount::applyDiscount)
35+
// .collect(Collectors.toList());
36+
37+
3038
return shops.stream()
3139
.map(shop -> shop.getPrice(product))
3240
.map(Quote::parse)
@@ -42,10 +50,14 @@ public List<String> findPricesParallel(String product) {
4250
.collect(Collectors.toList());
4351
}
4452

45-
public List<String> findPricesFuture(String product) {
53+
/**
54+
* 两个延时任务A,B,B的参数是A的结果,简单的说A与B有依赖的关系
55+
* @param product
56+
* @return
57+
*/
58+
public List<String> findPricesFuture(String product) {
4659
List<CompletableFuture<String>> priceFutures = findPricesStream(product)
47-
.collect(Collectors.<CompletableFuture<String>>toList());
48-
60+
.collect(Collectors.toList());
4961
return priceFutures.stream()
5062
.map(CompletableFuture::join)
5163
.collect(Collectors.toList());
@@ -67,4 +79,15 @@ public void printPricesStream(String product) {
6779
System.out.println("All shops have now responded in " + ((System.nanoTime() - start) / 1_000_000) + " msecs");
6880
}
6981

82+
public void fun1143(String product) {
83+
List<CompletableFuture<String>> priceFutures = shops.stream()
84+
.map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor))
85+
.map(future -> future.thenApply(Quote::parse)).map(future -> future.thenCompose(
86+
quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor)))
87+
.collect(Collectors.toList());
88+
priceFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());
89+
90+
91+
}
92+
7093
}

src/main/java/lambdasinaction/chap11/BestPriceFinderMain.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class BestPriceFinderMain {
88
private static BestPriceFinder bestPriceFinder = new BestPriceFinder();
99

1010
public static void main(String[] args) {
11-
execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S"));
12-
execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S"));
13-
execute("composed CompletableFuture", () -> bestPriceFinder.findPricesFuture("myPhone27S"));
11+
// execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S"));
12+
// execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S"));
13+
// execute("composed CompletableFuture", () -> bestPriceFinder.findPricesFuture("myPhone27S"));
1414
bestPriceFinder.printPricesStream("myPhone27S");
1515
}
1616

src/main/java/lambdasinaction/chap11/Discount.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ private static double apply(double price, Code code) {
2222
delay();
2323
return format(price * (100 - code.percentage) / 100);
2424
}
25+
26+
27+
public static void main(String[] args) {
28+
29+
}
2530
}

src/main/java/lambdasinaction/chap11/Quote.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@
22

33
public class Quote {
44

5-
private final String shopName;
6-
private final double price;
7-
private final Discount.Code discountCode;
5+
private final String shopName;
6+
private final double price;
7+
private final Discount.Code discountCode;
88

9-
public Quote(String shopName, double price, Discount.Code discountCode) {
10-
this.shopName = shopName;
11-
this.price = price;
12-
this.discountCode = discountCode;
13-
}
9+
public Quote(String shopName, double price, Discount.Code discountCode) {
10+
this.shopName = shopName;
11+
this.price = price;
12+
this.discountCode = discountCode;
13+
}
1414

15-
public static Quote parse(String s) {
16-
String[] split = s.split(":");
17-
String shopName = split[0];
18-
double price = Double.parseDouble(split[1]);
19-
Discount.Code discountCode = Discount.Code.valueOf(split[2]);
20-
return new Quote(shopName, price, discountCode);
21-
}
15+
public static Quote parse(String s) {
16+
System.out.println("parse " + s);
17+
String[] split = s.split(":");
18+
String shopName = split[0];
19+
double price = Double.parseDouble(split[1]);
20+
Discount.Code discountCode = Discount.Code.valueOf(split[2]);
21+
return new Quote(shopName, price, discountCode);
22+
}
2223

23-
public String getShopName() {
24-
return shopName;
25-
}
24+
public String getShopName() {
25+
return shopName;
26+
}
2627

27-
public double getPrice() {
28-
return price;
29-
}
28+
public double getPrice() {
29+
return price;
30+
}
3031

31-
public Discount.Code getDiscountCode() {
32-
return discountCode;
33-
}
32+
public Discount.Code getDiscountCode() {
33+
return discountCode;
34+
}
3435
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lambdasinaction.chap11.standard;
2+
3+
public class Resume {
4+
private String company;
5+
6+
private String school;
7+
8+
private String skill;
9+
10+
private String title;
11+
12+
public String getCompany() {
13+
return company;
14+
}
15+
16+
public void setCompany(String company) {
17+
this.company = company;
18+
}
19+
20+
public String getSchool() {
21+
return school;
22+
}
23+
24+
public void setSchool(String school) {
25+
this.school = school;
26+
}
27+
28+
public String getSkill() {
29+
return skill;
30+
}
31+
32+
public void setSkill(String skill) {
33+
this.skill = skill;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public void setTitle(String title) {
41+
this.title = title;
42+
}
43+
44+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

src/main/java/lambdasinaction/chap11/v1/BestPriceFinderMain.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public class BestPriceFinderMain {
88
private static BestPriceFinder bestPriceFinder = new BestPriceFinder();
99

1010
public static void main(String[] args) {
11-
execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S"));
12-
execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S"));
11+
// execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S"));
12+
// execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S"));
1313
execute("composed CompletableFuture", () -> bestPriceFinder.findPricesFuture("myPhone27S"));
14-
execute("combined USD CompletableFuture", () -> bestPriceFinder.findPricesInUSD("myPhone27S"));
15-
execute("combined USD CompletableFuture v2", () -> bestPriceFinder.findPricesInUSD2("myPhone27S"));
16-
execute("combined USD CompletableFuture v3", () -> bestPriceFinder.findPricesInUSD3("myPhone27S"));
14+
// execute("combined USD CompletableFuture", () -> bestPriceFinder.findPricesInUSD("myPhone27S"));
15+
// execute("combined USD CompletableFuture v2", () -> bestPriceFinder.findPricesInUSD2("myPhone27S"));
16+
// execute("combined USD CompletableFuture v3", () -> bestPriceFinder.findPricesInUSD3("myPhone27S"));
1717
}
1818

1919
private static void execute(String msg, Supplier<List<String>> s) {

src/main/java/lambdasinaction/chap11/v1/Shop.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ public String getName() {
3838
return name;
3939
}
4040

41+
42+
public static void main(String[] args) {
43+
// System.out.println(new Shop("yonwang").getPrice("apple"));
44+
System.out.println(new Shop("yongwang").getPriceAsync("origin"));
45+
46+
}
47+
4148
}

src/main/java/lambdasinaction/chap12/DateTimeExamples.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ protected DateFormat initialValue() {
3232
};
3333

3434
public static void main(String[] args) {
35-
useOldDate();
35+
// useOldDate();
3636
useLocalDate();
37-
useTemporalAdjuster();
38-
useDateFormatter();
37+
// useTemporalAdjuster();
38+
// useDateFormatter();
3939
}
4040

4141
private static void useOldDate() {
@@ -154,4 +154,8 @@ private static void useDateFormatter() {
154154
System.out.println(date.format(complexFormatter));
155155
}
156156

157+
private static void fun1211() {
158+
LocalDate localDate = LocalDate.of(2014, 10, 15);
159+
}
160+
157161
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package lambdasinaction.chap9;
2+
3+
public class Body implements Sized {
4+
5+
public static void main(String[] args) {
6+
System.out.println(new Body().size());
7+
}
8+
9+
@Override
10+
public int size() {
11+
return 20;
12+
}
13+
14+
@Override
15+
public boolean isEmpty() {
16+
return false;
17+
}
18+
}

0 commit comments

Comments
 (0)