Skip to content

Commit c33dfb1

Browse files
author
Ashley Frieze
committed
Add chapters 13 and 22
1 parent 30545d2 commit c33dfb1

File tree

9 files changed

+374
-0
lines changed

9 files changed

+374
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+

chapter13/Exercise1.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.packt.java.functional;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Exercise1 {
7+
8+
public static void main(String[] args) {
9+
System.out.println(sum(2,3));
10+
System.out.println(sum(2,3));
11+
System.out.println(sum(2,3));
12+
13+
System.out.println(sum(1, 2, 3, 4));
14+
}
15+
16+
static int sum(int ... values) {
17+
int total = 0;
18+
for (int value:values) {
19+
total += value;
20+
}
21+
return total;
22+
}
23+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.packt.java.functional;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Objects;
7+
8+
public class Exercise2 {
9+
public static void main(String[] args) {
10+
ShoppingCart myFirstCart = new ShoppingCart(new ArrayList<>());
11+
ShoppingCart mySecondCart = myFirstCart.addItem(
12+
new ShoppingItem("Chair", 150));
13+
ShoppingCart myThirdCart = mySecondCart.addItem(
14+
new ShoppingItem("Table",350));
15+
16+
System.out.println("First " + myFirstCart);
17+
System.out.println("Second " + mySecondCart);
18+
System.out.println("Third " + myThirdCart);
19+
20+
ShoppingCart myFourthCart = myThirdCart.removeItem(
21+
new ShoppingItem("Table",350));
22+
23+
System.out.println("Fourth " + myFourthCart);
24+
System.out.println("Third " + myThirdCart);
25+
26+
ShoppingCart myBulkCart = new ShoppingCart(new ArrayList<>());
27+
ShoppingCart myBulkWithTwoIn = myBulkCart.addItem(
28+
new ShoppingItem("Chair", 150),
29+
new ShoppingItem("Table", 350));
30+
31+
System.out.println("Bulk " + myBulkWithTwoIn);
32+
}
33+
34+
private static final class ShoppingCart {
35+
private final List<ShoppingItem> mShoppingList;
36+
37+
public ShoppingCart(List<ShoppingItem> mShoppingList) {
38+
this.mShoppingList = Collections.unmodifiableList(mShoppingList);
39+
}
40+
41+
public ShoppingCart addItem(ShoppingItem ... items) {
42+
List<ShoppingItem> newList = new ArrayList<>(mShoppingList);
43+
for (ShoppingItem item : items) {
44+
newList.add(item);
45+
}
46+
return new ShoppingCart(newList);
47+
}
48+
49+
public ShoppingCart addItem(ShoppingItem item, int quantity) {
50+
List<ShoppingItem> newList = new ArrayList<>(mShoppingList);
51+
for (int i=0; i<quantity; i++) {
52+
newList.add(item);
53+
}
54+
return new ShoppingCart(newList);
55+
}
56+
57+
public ShoppingCart removeItem(ShoppingItem item, int quantity) {
58+
List<ShoppingItem> newList = new ArrayList<>(mShoppingList);
59+
for (int i=0; i<quantity; i++) {
60+
newList.remove(item);
61+
}
62+
return new ShoppingCart(newList);
63+
}
64+
65+
public ShoppingCart removeItem(ShoppingItem item) {
66+
List<ShoppingItem> newList = new ArrayList<>(mShoppingList);
67+
newList.remove(item);
68+
return new ShoppingCart(newList);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "ShoppingCart{" +
74+
"mShoppingList=" + mShoppingList +
75+
'}';
76+
}
77+
}
78+
79+
private static final class ShoppingItem {
80+
private final String name;
81+
private final int price;
82+
83+
public ShoppingItem(String name, int price) {
84+
this.name = name;
85+
this.price = price;
86+
}
87+
88+
@Override
89+
public boolean equals(Object o) {
90+
if (this == o) {
91+
return true;
92+
}
93+
if (o == null || getClass() != o.getClass()) {
94+
return false;
95+
}
96+
ShoppingItem that = (ShoppingItem) o;
97+
return price == that.price &&
98+
Objects.equals(name, that.name);
99+
}
100+
101+
@Override
102+
public int hashCode() {
103+
return Objects.hash(name, price);
104+
}
105+
106+
@Override
107+
public String toString() {
108+
return "ShoppingItem{" +
109+
"name='" + name + '\'' +
110+
", price=" + price +
111+
'}';
112+
}
113+
}
114+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.packt.java.functional;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Exercise3 {
8+
public static void main(String[] args) {
9+
List<ShoppingItem> books = new ArrayList<>();
10+
books.add(new ShoppingItem("Java Fundamentals", 10));
11+
books.add(new ShoppingItem("Java 11 Quick Start", 11));
12+
13+
List<ShoppingItem> immutableCopy = List.copyOf(books);
14+
List<ShoppingItem> unmodifiableCopy = Collections.unmodifiableList(books);
15+
16+
System.out.println(immutableCopy);
17+
System.out.println(unmodifiableCopy);
18+
19+
books.remove(0);
20+
21+
System.out.println(immutableCopy);
22+
System.out.println(unmodifiableCopy);
23+
}
24+
25+
private static final class ShoppingCart {
26+
private final List<ShoppingItem> mShoppingList;
27+
28+
public ShoppingCart(List<ShoppingItem> mShoppingList) {
29+
this.mShoppingList = List.copyOf(mShoppingList);
30+
}
31+
32+
public ShoppingCart addItem(ShoppingItem item) {
33+
List<ShoppingItem> newList = new ArrayList<>(mShoppingList);
34+
newList.add(item);
35+
return new ShoppingCart(newList);
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "ShoppingCart{" +
41+
"mShoppingList=" + mShoppingList +
42+
'}';
43+
}
44+
}
45+
46+
private static final class ShoppingItem {
47+
private final String name;
48+
private final int price;
49+
50+
public ShoppingItem(String name, int price) {
51+
this.name = name;
52+
this.price = price;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "ShoppingItem{" +
58+
"name='" + name + '\'' +
59+
", price=" + price +
60+
'}';
61+
}
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.packt.java.functional;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
public class Exercise4 {
8+
public static void main(String[] args) {
9+
List<Tire> tires = List.of(
10+
new Tire(17),
11+
new Tire(16),
12+
new Tire(18),
13+
new Tire(14),
14+
new Tire(15),
15+
new Tire(16)
16+
);
17+
18+
List<Tire> sorted = getSortedList(tires);
19+
20+
System.out.println(sorted);
21+
}
22+
23+
private static List<Tire> getSortedList(List<Tire> tires) {
24+
List<Tire> sorted = new ArrayList<>(tires);
25+
sorted.sort((t1, t2) -> t2.size - t1.size);
26+
return sorted;
27+
}
28+
29+
public static final class Tire {
30+
private final int size;
31+
32+
public Tire(int size) {
33+
this.size = size;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return String.valueOf(size);
39+
}
40+
}
41+
}

chapter22/Threads.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.packt.java.threads;
2+
3+
import static java.lang.Thread.yield;
4+
import static java.util.Arrays.asList;
5+
6+
public class Main {
7+
static class MyOwnThread implements Runnable {
8+
public void run() {
9+
System.out.println(Thread.currentThread().getName() +
10+
" running");
11+
System.out.println(Thread.currentThread().getName() +
12+
": ID " +
13+
Thread.currentThread().getId());
14+
System.out.println(Thread.currentThread().getName() +
15+
": Priority " +
16+
Thread.currentThread().getPriority());
17+
18+
for (int i = 0; i < 100; i++) {
19+
yield();
20+
System.out.println(Thread.currentThread().getName() +
21+
" count: " + i);
22+
}
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
Thread threadA = new Thread(new MyOwnThread());
28+
threadA.setName("A");
29+
threadA.setPriority(Thread.MAX_PRIORITY);
30+
Thread threadB = new Thread(new MyOwnThread());
31+
threadB.setName("B");
32+
threadB.setPriority(Thread.MIN_PRIORITY);
33+
Thread threadC = new Thread(new MyOwnThread());
34+
threadC.setName("C");
35+
threadC.setPriority(Thread.MIN_PRIORITY);
36+
Thread threadD = new Thread(new MyOwnThread());
37+
threadD.setName("D");
38+
threadD.setPriority(Thread.MIN_PRIORITY);
39+
Thread threadE = new Thread(new MyOwnThread());
40+
threadE.setName("E");
41+
threadE.setPriority(Thread.MIN_PRIORITY);
42+
threadA.start();
43+
threadB.start();
44+
threadC.start();
45+
threadD.start();
46+
threadE.start();
47+
}
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.packt.java.threads;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
7+
public class Pool {
8+
static class MyThread implements Runnable {
9+
private String name;
10+
11+
MyThread (String name) {
12+
this.name = name;
13+
}
14+
15+
public void pause(int sleepTime) {
16+
try {
17+
Thread.sleep(sleepTime);
18+
} catch (InterruptedException ie) {
19+
System.out.println(name + " : Exception: " + ie.getMessage());
20+
}
21+
}
22+
23+
public void run() {
24+
System.out.println(name + " : start");
25+
26+
int max = new Random().nextInt(9) + 1;
27+
for (int i = 0; i < max; i++) {
28+
System.out.println(name + " : operation: " + i + " of " + max);
29+
pause(100);
30+
}
31+
System.out.println(name + " : stop");
32+
}
33+
}
34+
35+
public static void main(String[] args) {
36+
int POOL_SIZE = 5;
37+
int TOTAL_THREADS = 10;
38+
39+
// create an array of threads
40+
Runnable[] threads = new MyThread[TOTAL_THREADS];
41+
42+
// systematically create threads named A, B, C ...
43+
for(int i = 0; i < threads.length; i++) {
44+
threads[i] = new MyThread(Character.toString((char)('A' + i)));
45+
}
46+
47+
// construct the Thread Pool
48+
ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE);
49+
50+
// send the threads to the pool and let it run them
51+
for (int i = 0; i < threads.length; i++) {
52+
pool.execute(threads[i]);
53+
}
54+
55+
// end
56+
pool.shutdown();
57+
System.out.println("the end, my friend");
58+
}
59+
}

0 commit comments

Comments
 (0)