Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 31, 2020
2 parents cdf2dec + 7c165bf commit 506a574
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/main/java/org/cactoos/experimental/Threads.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
*
* @param <T> The type of task result item.
* @since 1.0.0
* @todo #972:30min j.u.c.ExecutorService#invokeAll(java.util.Collection) should
* be invoked with timeout. User should have the opportunity to pass the
* timeout argument. We need a new decorator which supports the timeout.
*/
public final class Threads<T> extends IterableEnvelope<T> {

Expand Down
162 changes: 162 additions & 0 deletions src/main/java/org/cactoos/experimental/Timed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.experimental;

import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.collection.Mapped;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;

/**
* Allows to execute the tasks concurrently within given timeout.
*
* @param <T> The type of task result item.
* @since 1.0.0
*/
public final class Timed<T> extends IterableEnvelope<T> {

/**
* Ctor.
* @param exc The executor.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
* @param tasks The tasks to be executed concurrently.
* @checkstyle ParameterNumberCheck (5 lines)
*/
@SafeVarargs
public Timed(
final ExecutorService exc,
final long timeout,
final TimeUnit unit,
final Scalar<T>... tasks
) {
this(exc, new IterableOf<>(tasks), timeout, unit);
}

/**
* Ctor.
* @param exc The executor.
* @param tasks The tasks to be executed concurrently.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
* @checkstyle ParameterNumberCheck (5 lines)
*/
public Timed(
final ExecutorService exc,
final Iterable<Scalar<T>> tasks,
final long timeout,
final TimeUnit unit
) {
this(
input -> exc.invokeAll(input, timeout, unit),
tasks
);
}

/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
* @param tasks The tasks to be executed concurrently.
* @see Executors#newFixedThreadPool(int)
* @checkstyle ParameterNumberCheck (5 lines)
*/
@SafeVarargs
public Timed(
final int threads,
final long timeout,
final TimeUnit unit,
final Scalar<T>... tasks
) {
this(threads, new IterableOf<>(tasks), timeout, unit);
}

/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param tasks The tasks to be executed concurrently.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
* @checkstyle IndentationCheck (20 lines)
* @checkstyle ParameterNumberCheck (5 lines)
*/
public Timed(
final int threads,
final Iterable<Scalar<T>> tasks,
final long timeout,
final TimeUnit unit
) {
this(
todo -> {
final ExecutorService executor = Executors.newFixedThreadPool(
threads
);
try {
return executor.invokeAll(todo, timeout, unit);
} finally {
executor.shutdown();
}
},
tasks
);
}

/**
* Ctor.
* @param fnc The function to map each task into {@link Future}.
* @param tasks The tasks to be executed concurrently.
* @checkstyle IllegalCatchCheck (20 lines)
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private Timed(
final Func<Collection<Callable<T>>, Collection<Future<T>>> fnc,
final Iterable<Scalar<T>> tasks
) {
super(
new IterableOf<>(
() -> {
try {
return new Mapped<>(
Future::get,
fnc.apply(new Mapped<>(task -> task::value, tasks))
).iterator();
} catch (final Exception exp) {
throw new CompletionException(exp);
}
}
)
);
}
}
Loading

2 comments on commit 506a574

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 506a574 Jan 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 972-6ba919a9 disappeared from src/main/java/org/cactoos/experimental/Threads.java, that's why I closed #1000. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 506a574 Jan 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1000-1e922fa0 discovered in src/test/java/org/cactoos/experimental/TimedTest.java and submitted as #1277. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.