├── .idea ├── vcs.xml ├── encodings.xml ├── misc.xml └── workspace.xml ├── src └── main │ └── java │ ├── helloworldprinter │ ├── Main.java │ └── HelloWorldPrinter.java │ ├── helloworldprinterrunnable │ ├── Main.java │ └── HelloWorldPrinter.java │ ├── printtillhundred │ ├── Main.java │ └── PrintNumber.java │ ├── simpleprogram │ └── Main.java │ ├── printtillhundredexecutors │ ├── PrintNumber.java │ └── Main.java │ └── mergesortmultithreaded │ ├── Main.java │ └── Sorter.java ├── .gitignore └── pom.xml /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/java/helloworldprinter/Main.java: -------------------------------------------------------------------------------- 1 | package helloworldprinter; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Thread t = new Thread(new HelloWorldPrinter()); 7 | t.start(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/helloworldprinter/HelloWorldPrinter.java: -------------------------------------------------------------------------------- 1 | package helloworldprinter; 2 | 3 | public class HelloWorldPrinter extends Thread { 4 | 5 | public void run() { 6 | System.out.println("Hello World " + Thread.currentThread().getName()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/helloworldprinterrunnable/Main.java: -------------------------------------------------------------------------------- 1 | package helloworldprinterrunnable; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Thread t = new Thread(new HelloWorldPrinter()); 7 | t.start(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/helloworldprinterrunnable/HelloWorldPrinter.java: -------------------------------------------------------------------------------- 1 | package helloworldprinterrunnable; 2 | 3 | public class HelloWorldPrinter implements Runnable { 4 | 5 | public void run() { 6 | System.out.println("Hello World " + Thread.currentThread().getName()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/printtillhundred/Main.java: -------------------------------------------------------------------------------- 1 | package printtillhundred; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | for (int i = 1; i <= 100; i++) { 7 | Thread t = new Thread(new PrintNumber(i)); 8 | t.start(); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/simpleprogram/Main.java: -------------------------------------------------------------------------------- 1 | package simpleprogram; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("Hello World " + Thread.currentThread().getName()); 7 | } 8 | } 9 | 10 | // Assignment 1 11 | // Create a program to print hello world 12 | // but from a different thread 13 | -------------------------------------------------------------------------------- /src/main/java/printtillhundred/PrintNumber.java: -------------------------------------------------------------------------------- 1 | package printtillhundred; 2 | 3 | public class PrintNumber implements Runnable { 4 | private int numberToPrint; 5 | 6 | public PrintNumber(int numberToPrint) { 7 | this.numberToPrint = numberToPrint; 8 | } 9 | 10 | @Override 11 | public void run() { 12 | System.out.println(numberToPrint + " Thread: " + Thread.currentThread().getName()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/printtillhundredexecutors/PrintNumber.java: -------------------------------------------------------------------------------- 1 | package printtillhundredexecutors; 2 | 3 | public class PrintNumber implements Runnable { 4 | private int numberToPrint; 5 | 6 | public PrintNumber(int numberToPrint) { 7 | this.numberToPrint = numberToPrint; 8 | } 9 | 10 | @Override 11 | public void run() { 12 | System.out.println(numberToPrint + " Thread: " + Thread.currentThread().getName()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/printtillhundredexecutors/Main.java: -------------------------------------------------------------------------------- 1 | package printtillhundredexecutors; 2 | 3 | import java.util.concurrent.Executor; 4 | import java.util.concurrent.ExecutorService; 5 | import java.util.concurrent.Executors; 6 | 7 | public class Main { 8 | 9 | public static void main(String[] args) { 10 | Executor executor = Executors.newFixedThreadPool(10); 11 | 12 | for (int i = 1; i <= 100; i++) { 13 | if (i == 50) { 14 | System.out.println("STOP"); 15 | } 16 | 17 | executor.execute(new PrintNumber(i)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.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 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | threadsMasterclass 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 19 13 | 19 14 | UTF-8 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/mergesortmultithreaded/Main.java: -------------------------------------------------------------------------------- 1 | package mergesortmultithreaded; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.List; 5 | import java.util.concurrent.ExecutionException; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.Future; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) throws ExecutionException, InterruptedException { 13 | List numbers = List.of( 14 | 7, 2, 9, 1, 5, 6 15 | ); 16 | 17 | ExecutorService executorService = Executors.newSingleThreadExecutor(); 18 | Future> sortedArrayFuture = executorService.submit(new Sorter(numbers)); 19 | 20 | List sortedArray = sortedArrayFuture.get(); 21 | 22 | for (Integer in: sortedArray) { 23 | System.out.println(in); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/mergesortmultithreaded/Sorter.java: -------------------------------------------------------------------------------- 1 | package mergesortmultithreaded; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.*; 6 | 7 | // Generics/ Templates 8 | public class Sorter implements Callable> { 9 | private List arrayToSort; 10 | 11 | public Sorter(List arrayToSort) { 12 | this.arrayToSort = arrayToSort; 13 | } 14 | 15 | @Override 16 | public List call() throws Exception { 17 | if (arrayToSort.size() <= 1) { // 7, 2, 9, 1, 5, 6 18 | return arrayToSort; 19 | } 20 | 21 | // [7, 2, 9, 1, 5, 6] 22 | int mid = arrayToSort.size()/ 2; // 3 23 | 24 | List leftArray = new ArrayList<>(); 25 | List rightArray = new ArrayList<>(); 26 | 27 | // 7, 2, 9 28 | for (int i = 0; i < mid; ++i) { 29 | leftArray.add(arrayToSort.get(i)); 30 | } 31 | 32 | // 1, 5, 6 33 | for (int i = mid; i < arrayToSort.size(); ++i) { 34 | rightArray.add(arrayToSort.get(i)); 35 | } 36 | 37 | ExecutorService executor = Executors.newCachedThreadPool(); 38 | 39 | Future> leftSortedArrayFuture = executor.submit(new Sorter(leftArray)); 40 | Future> rightSortedArrayFuture = executor.submit(new Sorter(rightArray)); 41 | 42 | List leftSortedArray = leftSortedArrayFuture.get(); 43 | List rightSortedArray = rightSortedArrayFuture.get(); 44 | 45 | List mergedArray = new ArrayList<>(); 46 | 47 | int i = 0; 48 | int j = 0; 49 | 50 | while (i < leftSortedArray.size() && j < rightSortedArray.size()) { 51 | if (leftSortedArray.get(i) < rightSortedArray.get(j)) { 52 | mergedArray.add(leftSortedArray.get(i)); 53 | i++; 54 | } else { 55 | mergedArray.add(rightSortedArray.get(j)); 56 | j++; 57 | } 58 | } 59 | 60 | while (i < leftSortedArray.size()) { 61 | mergedArray.add(leftSortedArray.get(i)); 62 | i++; 63 | } 64 | 65 | while (j < rightSortedArray.size()) { 66 | mergedArray.add(rightSortedArray.get(j)); 67 | j++; 68 | } 69 | 70 | return mergedArray; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 18 | 19 | 24 | 25 | 26 | 28 | 29 | 31 | 32 | 33 | 34 | 35 | 36 | 39 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 69 | 70 | 82 | 83 | 95 | 96 | 108 | 109 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 1661267627776 136 | 141 | 142 | 143 | 144 | 146 | 147 | 156 | 157 | 158 | 159 | 160 | 161 | file://$PROJECT_DIR$/src/main/java/simpleprogram/Main.java 162 | 5 163 | 165 | 166 | file://$PROJECT_DIR$/src/main/java/helloworldprinter/HelloWorldPrinter.java 167 | 5 168 | 170 | 171 | file://$PROJECT_DIR$/src/main/java/printtillhundredexecutors/Main.java 172 | 13 173 | 175 | 176 | file://$PROJECT_DIR$/src/main/java/mergesortmultithreaded/Main.java 177 | 11 178 | 179 | 182 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | --------------------------------------------------------------------------------