Skip to content

Commit

Permalink
Merge branch 'release/0.2.8-final'
Browse files Browse the repository at this point in the history
  • Loading branch information
waketraindev committed Dec 20, 2024
2 parents 2480ca0 + 923c5ff commit 7da3d36
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 13 additions & 10 deletions src/main/java/wtd/slotsengine/utils/RunGenerateReels.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ public class RunGenerateReels {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
log.info("Starting ReelOptimizer");
ReelOptimizer op = new ReelOptimizer(1024, 0.98);
op.setFoundBestCallback((rtp, reel) -> log.info("RTP: {} tSize: {} = {}", rtp, reel.size(), reel));
op.run(new TimeStopCondition(1, TimeUnit.MINUTES));
VirtualReel best = op.getBestReel();
long deltaTime = System.currentTimeMillis() - startTime;
log.info("Optimizer run finished.");
log.info("Reel: {}", best.toString());
log.info("RTP: {}", op.getBestRtp());
log.info("Size: {}", best.size());
log.info("Elapsed time: {}", Duration.ofMillis(deltaTime));
try (ReelOptimizer op = new ReelOptimizer(1024, 0.98)) {
op.setFoundBestCallback((rtp, reel) -> log.info("RTP: {} tSize: {} = {}", rtp, reel.size(), reel));
op.run(new TimeStopCondition(1, TimeUnit.MINUTES));
VirtualReel best = op.getBestReel();
long deltaTime = System.currentTimeMillis() - startTime;
log.info("Optimizer run finished.");
log.info("Reel: {}", best.toString());
log.info("RTP: {}", op.getBestRtp());
log.info("Size: {}", best.size());
log.info("Elapsed time: {}", Duration.ofMillis(deltaTime));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
18 changes: 16 additions & 2 deletions src/main/java/wtd/slotsengine/utils/generator/ReelOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* when a new best configuration is found. It is designed to handle parallel processing using an
* internal thread pool.
*/
public final class ReelOptimizer {
public final class ReelOptimizer implements AutoCloseable {
private final ExecutorService workPool = Executors.newWorkStealingPool();
private final double[] history;
private final int historySize;
Expand All @@ -37,6 +37,21 @@ public ReelOptimizer(int historySize, double targetRtp) {
this.targetRtp = targetRtp;
}

/**
* Closes the ReelOptimizer by shutting down the associated work pool.
* <p>
* This method ensures that the thread pool used for reel generation and optimization
* is properly terminated, preventing resource leaks and allowing graceful cleanup
* of resources. It should be invoked when the optimization process is completed or
* when the ReelOptimizer instance is no longer in use.
*
* @throws Exception if an error occurs during the shutdown process of the work pool.
*/
@Override
public void close() throws Exception {
workPool.shutdown();
}

/**
* Executes a single-threaded optimization process by iterating reel generation
* and result processing until the given stopping condition is met.
Expand Down Expand Up @@ -97,7 +112,6 @@ public void run(final GenStopCondition stopCondition) {
throw new RuntimeException(e);
} finally {
generatingThread.interrupt();
workPool.shutdown();
}
}

Expand Down

0 comments on commit 7da3d36

Please sign in to comment.