Add ThreadPool
This commit is contained in:
parent
2a5ddf81bd
commit
ab5e888ea8
@ -5,7 +5,38 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
// JavaPipe.PipeMain();
|
JavaPipe.PipeMain();
|
||||||
TransferSpam.main();
|
//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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
42
src/main/java/Threadpool/ThreadPool.java
Normal file
42
src/main/java/Threadpool/ThreadPool.java
Normal file
@ -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<Future<Integer>> allResults = new Vector<>();
|
||||||
|
ExecutorService threadpool = newFixedThreadPool(threadPoolSize);
|
||||||
|
|
||||||
|
for (int i = 0; i < numberOfCalculations; i++) {
|
||||||
|
Future<Integer> oneResult = threadpool.submit(new Calculation());
|
||||||
|
allResults.add(oneResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Future<Integer> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package smartBank;
|
package smartBank;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class TransferSpam extends Thread{
|
public class TransferSpam extends Thread{
|
||||||
Bank bank;
|
Bank bank;
|
||||||
Account sender;
|
Account sender;
|
||||||
@ -15,17 +17,19 @@ public class TransferSpam extends Thread{
|
|||||||
public void run() {
|
public void run() {
|
||||||
for (int i = 0; i < 25; i++) {
|
for (int i = 0; i < 25; i++) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(0);
|
Thread.sleep((int) (Math.random()*500));
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (Account.getLowerIdAccount(this.sender, this.receiver)){
|
synchronized (bank){ // To make Console prettier? very bad to sync the actual transactions
|
||||||
synchronized (Account.getHigherIdAccount(this.sender, this.receiver)){
|
synchronized (Account.getLowerIdAccount(this.sender, this.receiver)) {
|
||||||
|
synchronized (Account.getHigherIdAccount(this.sender, this.receiver)) {
|
||||||
bank.transfer(this.sender, this.receiver, 1);
|
bank.transfer(this.sender, this.receiver, 1);
|
||||||
System.out.println(bank);
|
System.out.println(bank);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user