|
@@ -56,74 +56,4 @@ public final class Helper {
|
|
|
return 1 + ThreadLocalRandom.current().nextInt(10000);
|
|
|
}
|
|
|
|
|
|
- private static final int cpuCount = Runtime.getRuntime().availableProcessors();
|
|
|
-
|
|
|
- public static final Executor executor =
|
|
|
- Executors.newFixedThreadPool(2000,
|
|
|
- new ThreadFactory() {
|
|
|
- @Override
|
|
|
- public Thread newThread(Runnable r) {
|
|
|
- Thread t = new Thread(r);
|
|
|
- t.setDaemon(true);
|
|
|
- return t;
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- public static World selectWorld(DataSource ds) {
|
|
|
- try (final Connection connection = ds.getConnection()) {
|
|
|
- try (PreparedStatement statement = connection.prepareStatement(
|
|
|
- "SELECT * FROM world WHERE id = ?",
|
|
|
- ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
|
|
|
- statement.setInt(1, Helper.randomWorld());
|
|
|
- try (ResultSet resultSet = statement.executeQuery()) {
|
|
|
- resultSet.next();
|
|
|
- return new World(
|
|
|
- resultSet.getInt("id"),
|
|
|
- resultSet.getInt("randomNumber"));
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static World updateWorld(DataSource ds) {
|
|
|
- World world;
|
|
|
- try (final Connection connection = ds.getConnection()) {
|
|
|
- try (PreparedStatement update = connection.prepareStatement(
|
|
|
- "UPDATE world SET randomNumber = ? WHERE id= ?")) {
|
|
|
- try (PreparedStatement query = connection.prepareStatement(
|
|
|
- "SELECT * FROM world WHERE id = ?",
|
|
|
- ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
|
|
|
-
|
|
|
- query.setInt(1, Helper.randomWorld());
|
|
|
- try (ResultSet resultSet = query.executeQuery()) {
|
|
|
- resultSet.next();
|
|
|
- world = new World(
|
|
|
- resultSet.getInt("id"),
|
|
|
- resultSet.getInt("randomNumber"));
|
|
|
- }
|
|
|
- }
|
|
|
- world.randomNumber = Helper.randomWorld();
|
|
|
- update.setInt(1, world.randomNumber);
|
|
|
- update.setInt(2, world.id);
|
|
|
- update.executeUpdate();
|
|
|
- return world;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) {
|
|
|
- CompletableFuture<Void> allDoneFuture =
|
|
|
- CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
|
|
|
- return allDoneFuture.thenApply(v ->
|
|
|
- futures.stream().
|
|
|
- map(future -> future.join()).
|
|
|
- collect(Collectors.<T>toList())
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
}
|