Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef6a74bd82d6febc2ecdb138445fae82b9eaa080 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
//  ========================================================================
//  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 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;

/**
 * <p>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.</p>
 * <p>The strategy delegates the production of tasks to a {@link Producer}, and continues to
 * execute tasks until the producer continues to produce them.</p>
 */
public interface ExecutionStrategy
{
    /**
     * <p>Initiates (or resumes) the task production and execution.</p>
     * <p>This method guarantees that the task is never run by the
     * thread that called this method.</p>
     *
     * @see #execute()
     */
    public void dispatch();

    /**
     * <p>Initiates (or resumes) the task production and execution.</p>
     * <p>The produced task may be run by the same thread that called
     * this method.</p>
     *
     * @see #dispatch()
     */
    public void execute();

    /**
     * <p>A producer of {@link Runnable} tasks to run.</p>
     * <p>The {@link ExecutionStrategy} will repeatedly invoke {@link #produce()} until
     * the producer returns null, indicating that it has nothing more to produce.</p>
     * <p>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.</p>
     */
    public interface Producer
    {
        /**
         * <p>Produces a task to be executed.</p>
         *
         * @return a task to executed or null if there are no more tasks to execute
         */
        Runnable produce();
    }

    public static class Factory
    {
        private static final Logger LOG = Log.getLogger(Factory.class);

        public static ExecutionStrategy instanceFor(Producer producer, Executor executor)
        {
            // TODO remove this mechanism before release
            String strategy = System.getProperty(producer.getClass().getName()+".ExecutionStrategy");
            if (strategy!=null)
            {
                try
                {
                    Class<? extends ExecutionStrategy> c = Loader.loadClass(strategy);
                    Constructor<? extends ExecutionStrategy> 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);
        }
    }
}

Back to the top