All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.util.thread.ExecutionStrategy Maven / Gradle / Ivy

There is a newer version: 9.4.9.v20180320
Show newest version
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.util.thread;

import java.lang.reflect.Constructor;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;

import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume;

/**
 * 

An {@link ExecutionStrategy} executes {@link Runnable} tasks produced by a {@link Producer}. * The strategy to execute the task may vary depending on the implementation; the task may be * run in the calling thread, or in a new thread, etc.

*

The strategy delegates the production of tasks to a {@link Producer}, and continues to * execute tasks until the producer continues to produce them.

*/ public interface ExecutionStrategy { /** *

Initiates (or resumes) the task production and execution.

*

This method guarantees that the task is never run by the * thread that called this method.

* * @see #execute() */ public void dispatch(); /** *

Initiates (or resumes) the task production and execution.

*

The produced task may be run by the same thread that called * this method.

* * @see #dispatch() */ public void execute(); /** * A task that can handle {@link RejectedExecutionException} */ public interface Rejectable { public void reject(); } /** *

A producer of {@link Runnable} tasks to run.

*

The {@link ExecutionStrategy} will repeatedly invoke {@link #produce()} until * the producer returns null, indicating that it has nothing more to produce.

*

When no more tasks can be produced, implementations should arrange for the * {@link ExecutionStrategy} to be invoked again in case an external event resumes * the tasks production.

*/ public interface Producer { /** *

Produces a task to be executed.

* * @return a task to executed or null if there are no more tasks to execute */ Runnable produce(); } /** *

A factory for {@link ExecutionStrategy}.

*/ public static interface Factory { /** *

Creates a new {@link ExecutionStrategy}.

* * @param producer the execution strategy producer * @param executor the execution strategy executor * @return a new {@link ExecutionStrategy} */ public ExecutionStrategy newExecutionStrategy(Producer producer, Executor executor); /** * @return the default {@link ExecutionStrategy} */ public static Factory getDefault() { return DefaultExecutionStrategyFactory.INSTANCE; } /** * @param producer the execution strategy producer * @param executor the execution strategy executor * @deprecated use {@code getDefault().newExecutionStrategy(Producer, Executor)} instead */ @Deprecated public static ExecutionStrategy instanceFor(Producer producer, Executor executor) { return getDefault().newExecutionStrategy(producer, executor); } } public static class DefaultExecutionStrategyFactory implements Factory { private static final Logger LOG = Log.getLogger(Factory.class); private static final Factory INSTANCE = new DefaultExecutionStrategyFactory(); @Override public ExecutionStrategy newExecutionStrategy(Producer producer, Executor executor) { String strategy = System.getProperty(producer.getClass().getName() + ".ExecutionStrategy"); if (strategy != null) { try { Class c = Loader.loadClass(producer.getClass(), strategy); Constructor m = c.getConstructor(Producer.class, Executor.class); LOG.info("Use {} for {}", c.getSimpleName(), producer.getClass().getName()); return m.newInstance(producer, executor); } catch (Exception e) { LOG.warn(e); } } return new ExecuteProduceConsume(producer, executor); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy