Nope, you cannot remove it. The IntConsumer
requires an input int
parameter.
Assuming you have multiple places in code where you want to do the same thing, you can use the following technique instead.
Create an ExecutorService
and define a helper method for repeating a command:
public static ExecutorService service = Executors.newFixedThreadPool(4); // Or any executor service you want
public static void repeat(int times, Runnable command) {
for (int i = 0; i < n; i++)
service.execute(command);
}
};
Then use it as follows:
repeat(100, yourRunnable);
// or
repeat(100, () -> { /* do something here */ });
The repeat
method can be implemented using Stream API
but it does not guarantee parallel execution of your commands. From the other hand it is blocking and will wait for all the commands to be executed. This might be helpful in some cases:
public static void repeat(int times, Runnable command) {
IntStream.range(0, times).parallel().forEach(__ -> command.run());
};