samples/privilegedcreator/src/org/apidesign/privileged/api/Executors.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 25 May 2009 18:27:02 +0200
changeset 330 be82436a7a8b
parent 71 samples/privilegedcreator/src/api/Executors.java@a9bd40166b60
permissions -rw-r--r--
More code snippets for the mutex example
jtulach@71
     1
jtulach@330
     2
package org.apidesign.privileged.api;
jtulach@71
     3
jtulach@71
     4
import java.util.concurrent.Executor;
jtulach@71
     5
jtulach@71
     6
/**
jtulach@71
     7
 *
jtulach@71
     8
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@71
     9
 */
jtulach@71
    10
public final class Executors {
jtulach@71
    11
    /** let's prefer factory methods */
jtulach@71
    12
    private Executors() {
jtulach@71
    13
    }
jtulach@71
    14
    
jtulach@71
    15
    
jtulach@71
    16
    public static Executor create() {
jtulach@71
    17
        return new Simple();
jtulach@71
    18
    }
jtulach@71
    19
    
jtulach@71
    20
    public static Executor create(boolean fair) {
jtulach@71
    21
        Configuration conf = new Configuration();
jtulach@71
    22
        conf.setFair(fair);
jtulach@71
    23
        return new Fair(conf);
jtulach@71
    24
    }
jtulach@71
    25
jtulach@71
    26
    // BEGIN: design.less.privileged
jtulach@71
    27
    public static Executor create(Configuration config) {
jtulach@71
    28
        return new Fair(config);
jtulach@71
    29
    }
jtulach@71
    30
    
jtulach@71
    31
    public static final class Configuration {
jtulach@71
    32
        boolean fair;
jtulach@71
    33
        int maxWaiters = -1;
jtulach@71
    34
        
jtulach@71
    35
        public void setFair(boolean fair) {
jtulach@71
    36
            this.fair = fair;
jtulach@71
    37
        }
jtulach@71
    38
        public void setMaxWaiters(int max) {
jtulach@71
    39
            this.maxWaiters = max;
jtulach@71
    40
        }
jtulach@71
    41
    }
jtulach@71
    42
    // END: design.less.privileged
jtulach@71
    43
    
jtulach@71
    44
    private static final class Simple implements Executor {
jtulach@71
    45
        public synchronized void execute(Runnable command) {
jtulach@71
    46
            command.run();
jtulach@71
    47
        }
jtulach@71
    48
    }
jtulach@71
    49
    private static final class Fair implements Executor {
jtulach@71
    50
        private final Configuration conf;
jtulach@71
    51
        
jtulach@71
    52
        public Fair(Configuration conf) {
jtulach@71
    53
            this.conf = conf;
jtulach@71
    54
        }
jtulach@71
    55
        
jtulach@71
    56
        public void execute(Runnable command) {
jtulach@71
    57
            // TBD
jtulach@71
    58
        }
jtulach@71
    59
    }
jtulach@71
    60
}