-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
451 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.
506a574
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
972-6ba919a9
disappeared fromsrc/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.506a574
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
1000-1e922fa0
discovered insrc/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.