Skip to content

Commit

Permalink
Merge pull request #151 from phinner/fix/blocking-threads
Browse files Browse the repository at this point in the history
Fix argon and balloon blocking application shutdown
  • Loading branch information
firaja authored Apr 23, 2024
2 parents 462cf46 + 65c697d commit c21a064
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/password4j/Argon2Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class Argon2Function extends AbstractHashingFunction

if (parallelism >= 1)
{
service = Executors.newFixedThreadPool(Utils.AVAILABLE_PROCESSORS);
service = Utils.createExecutorService();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/password4j/BalloonHashingFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class BalloonHashingFunction extends AbstractHashingFunction
this.delta = delta;
if (parallelism > 1)
{
this.service = Executors.newFixedThreadPool(Utils.AVAILABLE_PROCESSORS);
this.service = Utils.createExecutorService();
}

}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/password4j/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -50,6 +53,10 @@ class Utils

private static final int[] FROM_BASE64 = new int[256];

private static final AtomicInteger THREAD_COUNTER = new AtomicInteger(1);

private static final ThreadGroup THREAD_GROUP = new ThreadGroup("Password4j Workers");

static
{
Arrays.fill(FROM_BASE64, -1);
Expand Down Expand Up @@ -655,6 +662,11 @@ static List<byte[]> split(byte[] array, byte delimiter) {
return byteArrays;
}



static ExecutorService createExecutorService() {
return Executors.newFixedThreadPool(AVAILABLE_PROCESSORS, runnable -> {
Thread thread = new Thread(THREAD_GROUP, runnable, "password4j-worker-" + THREAD_COUNTER.getAndIncrement());
thread.setDaemon(true);
return thread;
});
}
}

0 comments on commit c21a064

Please sign in to comment.