-
-
Notifications
You must be signed in to change notification settings - Fork 22
Performing tasks in parallel with different priorities
By using the BackgroundExecutor component you can launch different Runnables or Suppliers in a parallel way and wait for them starting or finishing. For obtaining threads the BackgroundExecutor uses the ThreadSupplier component which can be customized in the burningwave.static.properties file. The ThreadSupplier provides a fixed number of reusable threads indicated by the 'thread-supplier.max-poolable-threads-count' property and, if these threads have already been assigned, new non-reusable threads will be created whose quantity maximum is indicated by the 'thread-supplier.max-detached-threads-count' property. Once this limit is reached if the request for a new thread exceeds the waiting time indicated by the 'thread-supplier.poolable-thread-request-timeout' property, the ThreadSupplier will proceed to increase the limit indicated by the 'thread-supplier.max-detached-threads-count' property for the quantity indicated by the 'thread-supplier.max-detached-threads-count.increasing-step' property. Resetting the 'thread-supplier.max-detached-threads-count' property to its initial value, will occur gradually only when there have been no more waits on thread requests for an amount of time indicated by the 'thread-supplier.max-detached-threads-count.elapsed-time-threshold-from-last-increase-for-gradual-decreasing-to-initial-value' property.
import static org.burningwave.core.assembler.StaticComponentContainer.BackgroundExecutor;
import org.burningwave.core.ManagedLogger;
import org.burningwave.core.concurrent.QueuedTasksExecutor.ProducerTask;
import org.burningwave.core.concurrent.QueuedTasksExecutor.Task;
public class TaskLauncher implements ManagedLogger {
public void launch() {
ProducerTask<Long> taskOne = BackgroundExecutor.createTask(() -> {
Long startTime = System.currentTimeMillis();
logInfo("task one started");
synchronized (this) {
wait(5000);
}
Task internalTask = BackgroundExecutor.createTask(() -> {
logInfo("internal task started");
synchronized (this) {
wait(5000);
}
logInfo("internal task finished");
}, Thread.MAX_PRIORITY).submit();
internalTask.waitForFinish();
logInfo("task one finished");
return startTime;
}, Thread.MAX_PRIORITY).submit();
Task taskTwo = BackgroundExecutor.createTask(() -> {
logInfo("task two started and wait for task one finishing");
taskOne.waitForFinish();
logInfo("task two finished");
}, Thread.NORM_PRIORITY).submit();
ProducerTask<Long> taskThree = BackgroundExecutor.createTask(() -> {
logInfo("task three started and wait for task two finishing");
taskTwo.waitForFinish();
logInfo("task two finished");
return System.currentTimeMillis();
}, Thread.MIN_PRIORITY).submit();
taskThree.waitForFinish();
logInfo("Elapsed time: {}ms", taskThree.join() - taskOne.join());
}
public static void main(String[] args) {
new TaskLauncher().launch();
}
}
Burningwave core is a fully indipendent, advanced, free and open source Java frameworks building library that contains AN EXTREMELY POWERFUL CLASSPATH SCANNER.
To include Burningwave Core library in your projects simply use with Apache Maven:
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>12.65.2</version>
</dependency>
To use Burningwave Core as a Java module add the following to your module-info.java
:
requires org.burningwave.core;
ClassFactory
ClassHunter
- In depth look to and configuration guide
- USE CASE: retrieving all classes of the classpath
- USE CASE: retrieving all classes that implement one or more interfaces
- USE CASE: finding all classes that extend a base class
- USE CASE: searching for all classes that have package name that matches a regex
- USE CASE: finding all classes for module name (Java 9 and later)
- USE CASE: finding all annotated classes
- USE CASE: how to scan classes for specific annotations and collect its values
- USE CASE: searching for all classes with a constructor that takes a specific type as first parameter and with at least 2 methods that begin for a given string
- USE CASE: searching for all classes with methods whose name begins for a given string and that takes a specific type as its first parameter
- USE CASE: finding all classes that have at least 2 protected fields