├── src
└── main
│ └── java
│ ├── addersubtractor
│ ├── Count.java
│ ├── Adder.java
│ ├── Subtractor.java
│ └── Client.java
│ ├── printtillhundred
│ ├── Client.java
│ └── NumberPrinter.java
│ ├── introtothreads
│ └── Client.java
│ ├── executors
│ └── printtillhundred
│ │ ├── NumberPrinter.java
│ │ └── Client.java
│ ├── hellloworldprinter
│ ├── HelloWorldPrinter.java
│ └── Client.java
│ └── mergesort
│ ├── Client.java
│ └── MergeSorter.java
├── .idea
├── vcs.xml
├── encodings.xml
├── misc.xml
└── workspace.xml
├── .gitignore
└── pom.xml
/src/main/java/addersubtractor/Count.java:
--------------------------------------------------------------------------------
1 | package addersubtractor;
2 |
3 | public class Count {
4 | int value = 0;
5 | }
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/main/java/printtillhundred/Client.java:
--------------------------------------------------------------------------------
1 | package printtillhundred;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 | for (int i = 1; i <= 100; ++i) {
7 | NumberPrinter numberPrinter = new NumberPrinter(i);
8 | Thread t = new Thread(numberPrinter);
9 | t.start();
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/addersubtractor/Adder.java:
--------------------------------------------------------------------------------
1 | package addersubtractor;
2 |
3 | public class Adder implements Runnable {
4 | private Count count;
5 |
6 | public Adder(Count count) {
7 | this.count = count;
8 | }
9 |
10 | @Override
11 | public void run() {
12 | for(int i = 1; i <= 10000; ++i) {
13 | count.value += i;
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/addersubtractor/Subtractor.java:
--------------------------------------------------------------------------------
1 | package addersubtractor;
2 |
3 | public class Subtractor implements Runnable {
4 | private Count count;
5 |
6 | public Subtractor(Count count) {
7 | this.count = count;
8 | }
9 |
10 | @Override
11 | public void run() {
12 | for (int i = 1 ; i <= 10000; ++i) {
13 | count.value -= i;
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/introtothreads/Client.java:
--------------------------------------------------------------------------------
1 | package introtothreads;
2 |
3 | public class Client {
4 |
5 | public static void doSomething() {
6 | System.out.println("Doing something. Printed by thread: " + Thread.currentThread().getName());
7 | }
8 |
9 | public static void main(String[] args) {
10 | System.out.println("Hello World. Printed by thread: " + Thread.currentThread().getName());
11 | doSomething();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/printtillhundred/NumberPrinter.java:
--------------------------------------------------------------------------------
1 | package printtillhundred;
2 |
3 | public class NumberPrinter implements Runnable {
4 | private int numberToPrint;
5 |
6 | NumberPrinter(int numberToPrint) {
7 | this.numberToPrint = numberToPrint;
8 | }
9 |
10 | @Override
11 | public void run() {
12 | System.out.println("Printing " + this.numberToPrint + ". Printed by thread: " + Thread.currentThread().getName());
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/executors/printtillhundred/NumberPrinter.java:
--------------------------------------------------------------------------------
1 | package executors.printtillhundred;
2 |
3 | public class NumberPrinter implements Runnable {
4 | private int numberToPrint;
5 |
6 | NumberPrinter(int numberToPrint) {
7 | this.numberToPrint = numberToPrint;
8 | }
9 |
10 | @Override
11 | public void run() {
12 | System.out.println("Printing " + this.numberToPrint + ". Printed by thread: " + Thread.currentThread().getName());
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/hellloworldprinter/HelloWorldPrinter.java:
--------------------------------------------------------------------------------
1 | package hellloworldprinter;
2 |
3 | public class HelloWorldPrinter implements Runnable {
4 |
5 | public void doSomething() {
6 | System.out.println("Doing something. Printed by: " + Thread.currentThread().getName());
7 | }
8 |
9 | @Override
10 | public void run() {
11 | System.out.println("Hello World. Printed by: " + Thread.currentThread().getName());
12 | doSomething();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/hellloworldprinter/Client.java:
--------------------------------------------------------------------------------
1 | package hellloworldprinter;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 | System.out.println("Hello World. Printed by: " + Thread.currentThread().getName());
7 |
8 | HelloWorldPrinter helloWorldPrinter = new HelloWorldPrinter();
9 | Thread t = new Thread(helloWorldPrinter);
10 | t.start();
11 |
12 | // helloWorldPrinter.run();
13 | System.out.println("Before I end. Printed by: " + Thread.currentThread().getName());
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/addersubtractor/Client.java:
--------------------------------------------------------------------------------
1 | package addersubtractor;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 | Count count = new Count();
7 |
8 | Adder adder = new Adder(count);
9 | Subtractor subtractor = new Subtractor(count);
10 |
11 | Thread t1 = new Thread(adder);
12 | Thread t2 = new Thread(subtractor);
13 |
14 | t1.start();
15 | t2.start();
16 |
17 | t1.join();
18 | t2.join();
19 |
20 | System.out.println(count.value);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/modules.xml
8 | .idea/jarRepositories.xml
9 | .idea/compiler.xml
10 | .idea/libraries/
11 | *.iws
12 | *.iml
13 | *.ipr
14 |
15 | ### Eclipse ###
16 | .apt_generated
17 | .classpath
18 | .factorypath
19 | .project
20 | .settings
21 | .springBeans
22 | .sts4-cache
23 |
24 | ### NetBeans ###
25 | /nbproject/private/
26 | /nbbuild/
27 | /dist/
28 | /nbdist/
29 | /.nb-gradle/
30 | build/
31 | !**/src/main/**/build/
32 | !**/src/test/**/build/
33 |
34 | ### VS Code ###
35 | .vscode/
36 |
37 | ### Mac OS ###
38 | .DS_Store
--------------------------------------------------------------------------------
/src/main/java/executors/printtillhundred/Client.java:
--------------------------------------------------------------------------------
1 | package executors.printtillhundred;
2 |
3 | import java.util.concurrent.Executor;
4 | import java.util.concurrent.ExecutorService;
5 | import java.util.concurrent.Executors;
6 |
7 | public class Client {
8 |
9 | public static void main(String[] args) {
10 | ExecutorService executor = Executors.newCachedThreadPool();
11 |
12 | for (int i = 1; i <= 100; ++i) {
13 | if (i == 5 || i == 11 || i == 20) {
14 | System.out.println("Debug");
15 | }
16 | NumberPrinter numberPrinter = new NumberPrinter(i);
17 | executor.execute(numberPrinter);
18 | }
19 |
20 | executor.shutdown();
21 | // executor.
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/mergesort/Client.java:
--------------------------------------------------------------------------------
1 | package mergesort;
2 |
3 | import java.util.List;
4 | import java.util.concurrent.Executor;
5 | import java.util.concurrent.ExecutorService;
6 | import java.util.concurrent.Executors;
7 | import java.util.concurrent.Future;
8 |
9 | public class Client {
10 |
11 | public static void main(String[] args) throws Exception {
12 | List list = List.of(7, 3, 4, 1, 9, 8, 2, 6);
13 | ExecutorService executorService = Executors.newCachedThreadPool();
14 |
15 | MergeSorter mergeSorter = new MergeSorter(list, executorService);
16 |
17 | Future> sortedListFuture = executorService.submit(mergeSorter);
18 |
19 | List sortedList = sortedListFuture.get();
20 |
21 | System.out.println(sortedList);
22 |
23 | executorService.shutdown();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | threadsSep2022
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | org.apache.maven.plugins
14 | maven-compiler-plugin
15 |
16 | 18
17 | 18
18 |
19 |
20 |
21 |
22 |
23 |
24 | 19
25 | 19
26 | UTF-8
27 |
28 |
29 |
--------------------------------------------------------------------------------
/src/main/java/mergesort/MergeSorter.java:
--------------------------------------------------------------------------------
1 | package mergesort;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.concurrent.Callable;
7 | import java.util.concurrent.ExecutorService;
8 | import java.util.concurrent.Executors;
9 | import java.util.concurrent.Future;
10 |
11 | public class MergeSorter implements Callable> {
12 | private List arrayToSort;
13 | private ExecutorService executorService;
14 |
15 | public MergeSorter(List arrayToSort, ExecutorService executorService) {
16 | this.arrayToSort = arrayToSort;
17 | this.executorService = executorService;
18 | }
19 |
20 | @Override
21 | public List call() throws Exception {
22 | if (arrayToSort.size() <= 1) {
23 | return arrayToSort;
24 | }
25 |
26 | int mid = arrayToSort.size()/ 2; // 2 -> 1 3 -> 1
27 |
28 | List leftArray = new ArrayList<>();
29 |
30 | for (int i = 0; i < mid; ++i) {
31 | leftArray.add(arrayToSort.get(i));
32 | }
33 |
34 | List rightArray = new ArrayList<>();
35 |
36 | for (int i = mid; i < arrayToSort.size(); ++i) {
37 | rightArray.add(arrayToSort.get(i));
38 | }
39 |
40 | MergeSorter leftMergeSorter = new MergeSorter(leftArray, executorService);
41 | MergeSorter rightMergeSorter = new MergeSorter(rightArray, executorService);
42 |
43 | Future> leftSortedArrayFuture = executorService.submit(leftMergeSorter);
44 | Future> rightSortedArrayFuture = executorService.submit(rightMergeSorter);
45 |
46 | List sortedArray = new ArrayList<>();
47 |
48 | int i = 0;
49 | int j = 0;
50 |
51 | List leftSortedArray = leftSortedArrayFuture.get(); // code will not go to the next line till I have the sorted array
52 | List rightSortedArray = rightSortedArrayFuture.get();
53 |
54 | while (i < leftSortedArray.size() && j < rightSortedArray.size()) {
55 | if (leftSortedArray.get(i) < rightSortedArray.get(j)) {
56 | sortedArray.add(leftSortedArray.get(i));
57 | ++i;
58 | } else {
59 | sortedArray.add(rightSortedArray.get(j));
60 | ++j;
61 | }
62 | }
63 |
64 | while (i < leftSortedArray.size()) {
65 | sortedArray.add(leftSortedArray.get(i));
66 | ++i;
67 | }
68 |
69 | while (j < rightSortedArray.size()) {
70 | sortedArray.add(rightSortedArray.get(j));
71 | ++j;
72 | }
73 |
74 | return sortedArray;
75 | }
76 | }
77 |
78 | // Generics
79 | // List<>
80 | // HashMap<>
81 |
82 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 | 1662779604527
147 |
148 |
149 | 1662779604527
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
167 |
168 |
169 |
170 |
171 |
172 |
173 | file://$PROJECT_DIR$/src/main/java/introtothreads/Client.java
174 | 9
175 |
176 |
177 |
178 | file://$PROJECT_DIR$/src/main/java/executors/printtillhundred/Client.java
179 | 13
180 |
181 |
182 |
183 | file://$PROJECT_DIR$/src/main/java/mergesort/MergeSorter.java
184 | 22
185 |
186 |
187 |
188 |
189 |
190 |
--------------------------------------------------------------------------------