From ab5e888ea83969aaf9ebbd63f52bb2c96b68c033 Mon Sep 17 00:00:00 2001 From: Matti Date: Mon, 21 Oct 2024 08:48:49 +0200 Subject: [PATCH] Add ThreadPool --- src/main/java/Main.java | 35 +++++++++++++++++-- src/main/java/Threadpool/ThreadPool.java | 42 +++++++++++++++++++++++ src/main/java/smartBank/TransferSpam.java | 10 ++++-- 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Threadpool/ThreadPool.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 3bd8313..8618112 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -5,7 +5,38 @@ import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { -// JavaPipe.PipeMain(); - TransferSpam.main(); + JavaPipe.PipeMain(); + //TransferSpam.main(); + } +} + +class RecursiveMath{ + public static int add(int a, int b){ + if(b < 1){ + return a; + } + return add(a+1, b-1); + } + + public static int multiply(int a, int b) { + if (b < 0) { + return -multiply(a, -b); + } else if (b == 0) { + return 0; + } else { + return add(a, multiply(a, b - 1)); + } + } + + public static int expo(int a, int b){ + if (b==0){ + return 1; + }else if (b == 1) { + return a; + } + return multiply(a, expo(a, b-1)); + } + public static void main(String[] args) { + System.out.println(expo(2,6)); } } diff --git a/src/main/java/Threadpool/ThreadPool.java b/src/main/java/Threadpool/ThreadPool.java new file mode 100644 index 0000000..52cb31b --- /dev/null +++ b/src/main/java/Threadpool/ThreadPool.java @@ -0,0 +1,42 @@ +package Threadpool; + +import java.util.Vector; +import java.util.concurrent.*; +import static java.util.concurrent.Executors.newFixedThreadPool; + +public class ThreadPool { + private class Calculation implements Callable{ + @Override + public Object call() throws Exception { + Thread.sleep(1000); + return 42; + } + } + public int doCalculation() throws ExecutionException, InterruptedException { + int totalResult = 0; + int threadPoolSize = 5; + int numberOfCalculations = 10; + + Vector> allResults = new Vector<>(); + ExecutorService threadpool = newFixedThreadPool(threadPoolSize); + + for (int i = 0; i < numberOfCalculations; i++) { + Future oneResult = threadpool.submit(new Calculation()); + allResults.add(oneResult); + } + + for (Future result : allResults) { + totalResult += result.get(); + } + + threadpool.shutdown(); + return totalResult; + } + + public static void main(String[] args) throws ExecutionException, InterruptedException { + ThreadPool test = new ThreadPool(); + System.out.println("Calculation started"); + int result = test.doCalculation(); + System.out.println("Result: " + result); + } +} diff --git a/src/main/java/smartBank/TransferSpam.java b/src/main/java/smartBank/TransferSpam.java index 971cfeb..b19edf2 100644 --- a/src/main/java/smartBank/TransferSpam.java +++ b/src/main/java/smartBank/TransferSpam.java @@ -1,5 +1,7 @@ package smartBank; +import java.util.Random; + public class TransferSpam extends Thread{ Bank bank; Account sender; @@ -15,17 +17,19 @@ public class TransferSpam extends Thread{ public void run() { for (int i = 0; i < 25; i++) { try { - Thread.sleep(0); + Thread.sleep((int) (Math.random()*500)); } catch (InterruptedException e) { throw new RuntimeException(e); } - synchronized (Account.getLowerIdAccount(this.sender, this.receiver)){ - synchronized (Account.getHigherIdAccount(this.sender, this.receiver)){ + synchronized (bank){ // To make Console prettier? very bad to sync the actual transactions + synchronized (Account.getLowerIdAccount(this.sender, this.receiver)) { + synchronized (Account.getHigherIdAccount(this.sender, this.receiver)) { bank.transfer(this.sender, this.receiver, 1); System.out.println(bank); } } + } } }