在Java中以不同的优先级并行执行任务


Java CompletableFuture的 一个小限制是无法选择分配给它们的线程的优先级,因此无法将所需的工作负载分配给cpu。在这种情况下,Burningwave Core库的BackgroundExecutor组件将为我们提供帮助,使我们能够并行启动不同的RunnableSuppliers,并等待它们开始或完成。为了获取线程,BackgroundExecutor使用ThreadSupplier组件,可以通过以下属性在burningwave.static.properties文件中对其进行配置:

thread-supplier.default-daemon-flag-value=true
thread-supplier.max-detached-threads-count=autodetect
thread-supplier.max-detached-threads-count.elapsed-time-threshold-from-last-increase-for-gradual-decreasing-to-initial-value=30000
thread-supplier.max-detached-threads-count.increasing-step=
thread-supplier.max-poolable-threads-count=autodetect
thread-supplier.poolable-thread-request-timeout=6000

ThreadSupplier提供固定数量的“ thread-supplier.max-poolable-threads-count ”属性指示的可重用线程,如果已经分配了这些线程,则将创建新的不可重用线程,其最大数量由表示。 ' thread-supplier.max-detached-threads-count '属性。如果对新线程的请求超过了“ thread-supplier.poolable-thread-request-timeout ”属性指示的等待时间,则达到此限制后,ThreadSupplier将继续增加“线程供应商”指示的限制。 “ max-detached-threads-count”属性,用于“ thread-supplier.max-detached-threads-count.increasing-step ”属性指示的数量。

仅当在' thread-supplier.max-所指示的时间段内不再等待线程请求时,才逐渐将'thread-supplier.max-detached-threads-count'属性重置为其初始值。从上次分离线程计数开始的时间阈值开始逐渐增加到初始值'属性。

现在让我们看一个有关如何创建任务的示例:

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 three 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();
    }

}


原文链接:http://codingdict.com