Add ThreadPool
This commit is contained in:
parent
2a5ddf81bd
commit
ab5e888ea8
@ -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));
|
||||
}
|
||||
}
|
||||
|
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;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class TransferSpam extends Thread{
|
||||
Bank bank;
|
||||
Account sender;
|
||||
@ -15,19 +17,21 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
Bank bank = new Bank();
|
||||
|
Loading…
Reference in New Issue
Block a user