Changeset bafef846


Ignore:
Timestamp:
Mar 22, 2012 7:53:05 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
cae9432
Parents:
db42a46c
Message:
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/I2PAppContext.java

    rdb42a46c rbafef846  
    3838import net.i2p.util.RandomSource;
    3939import net.i2p.util.SecureDirectory;
     40import net.i2p.util.SimpleScheduler;
     41import net.i2p.util.SimpleTimer;
     42import net.i2p.util.SimpleTimer2;
    4043import net.i2p.util.I2PProperties.I2PPropertyCallback;
    4144
     
    8790    private KeyGenerator _keyGenerator;
    8891    protected KeyRing _keyRing; // overridden in RouterContext
     92    private SimpleScheduler _simpleScheduler;
     93    private SimpleTimer _simpleTimer;
     94    private SimpleTimer2 _simpleTimer2;
    8995    private final PortMapper _portMapper;
    9096    private volatile boolean _statManagerInitialized;
     
    104110    private volatile boolean _keyGeneratorInitialized;
    105111    protected volatile boolean _keyRingInitialized; // used in RouterContext
     112    private volatile boolean _simpleSchedulerInitialized;
     113    private volatile boolean _simpleTimerInitialized;
     114    private volatile boolean _simpleTimer2Initialized;
    106115    protected final Set<Runnable> _shutdownTasks;
    107116    private File _baseDir;
     
    117126                         _lock9 = new Object(), _lock10 = new Object(), _lock11 = new Object(), _lock12 = new Object(),
    118127                         _lock13 = new Object(), _lock14 = new Object(), _lock15 = new Object(), _lock16 = new Object(),
    119                          _lock17 = new Object();
     128                         _lock17 = new Object(), _lock18 = new Object(), _lock19 = new Object(), _lock20 = new Object();
    120129
    121130    /**
     
    922931        return _portMapper;
    923932    }
     933
     934    /**
     935     * Use instead of SimpleScheduler.getInstance()
     936     * @since 0.9 to replace static instance in the class
     937     */
     938    public SimpleScheduler simpleScheduler() {
     939        if (!_simpleSchedulerInitialized)
     940            initializeSimpleScheduler();
     941        return _simpleScheduler;
     942    }
     943
     944    private void initializeSimpleScheduler() {
     945        synchronized (_lock18) {
     946            if (_simpleScheduler == null)
     947                _simpleScheduler = new SimpleScheduler(this);
     948            _simpleSchedulerInitialized = true;
     949        }
     950    }
     951
     952    /**
     953     * Use instead of SimpleTimer.getInstance()
     954     * @since 0.9 to replace static instance in the class
     955     */
     956    public SimpleTimer simpleTimer() {
     957        if (!_simpleTimerInitialized)
     958            initializeSimpleTimer();
     959        return _simpleTimer;
     960    }
     961
     962    private void initializeSimpleTimer() {
     963        synchronized (_lock19) {
     964            if (_simpleTimer == null)
     965                _simpleTimer = new SimpleTimer(this);
     966            _simpleTimerInitialized = true;
     967        }
     968    }
     969
     970    /**
     971     * Use instead of SimpleTimer2.getInstance()
     972     * @since 0.9 to replace static instance in the class
     973     */
     974    public SimpleTimer2 simpleTimer2() {
     975        if (!_simpleTimer2Initialized)
     976            initializeSimpleTimer2();
     977        return _simpleTimer2;
     978    }
     979
     980    private void initializeSimpleTimer2() {
     981        synchronized (_lock20) {
     982            if (_simpleTimer2 == null)
     983                _simpleTimer2 = new SimpleTimer2(this);
     984            _simpleTimer2Initialized = true;
     985        }
     986    }
    924987}
  • core/java/src/net/i2p/util/Executor.java

    rdb42a46c rbafef846  
    55import net.i2p.I2PAppContext;
    66
     7/**
     8 *  Deprecated - used only by SimpleTimer
     9 */
    710class Executor implements Runnable {
    811    private final I2PAppContext _context;
  • core/java/src/net/i2p/util/SimpleScheduler.java

    rdb42a46c rbafef846  
    2828 */
    2929public class SimpleScheduler {
    30     private static final SimpleScheduler _instance = new SimpleScheduler();
    31     public static SimpleScheduler getInstance() { return _instance; }
     30
     31    /**
     32     *  If you have a context, use context.simpleScheduler() instead
     33     */
     34    public static SimpleScheduler getInstance() {
     35        return I2PAppContext.getGlobalContext().simpleScheduler();
     36    }
     37
    3238    private static final int MIN_THREADS = 2;
    3339    private static final int MAX_THREADS = 4;
    34     private final I2PAppContext _context;
    3540    private final Log _log;
    3641    private final ScheduledThreadPoolExecutor _executor;
     
    3944    private final int _threads;
    4045
    41     protected SimpleScheduler() { this("SimpleScheduler"); }
    42     protected SimpleScheduler(String name) {
    43         _context = I2PAppContext.getGlobalContext();
    44         _log = _context.logManager().getLog(SimpleScheduler.class);
     46    /**
     47     *  To be instantiated by the context.
     48     *  Others should use context.simpleTimer() instead
     49     */
     50    public SimpleScheduler(I2PAppContext context) {
     51        this(context, "SimpleScheduler");
     52    }
     53
     54    /**
     55     *  To be instantiated by the context.
     56     *  Others should use context.simpleTimer() instead
     57     */
     58    private SimpleScheduler(I2PAppContext context, String name) {
     59        _log = context.logManager().getLog(SimpleScheduler.class);
    4560        _name = name;
    4661        long maxMemory = Runtime.getRuntime().maxMemory();
     
    5166        _executor.prestartAllCoreThreads();
    5267        // don't bother saving ref to remove hook if somebody else calls stop
    53         _context.addShutdownTask(new Shutdown());
     68        context.addShutdownTask(new Shutdown());
    5469    }
    5570   
  • core/java/src/net/i2p/util/SimpleTimer.java

    rdb42a46c rbafef846  
    1919 */
    2020public class SimpleTimer {
    21     private static final SimpleTimer _instance = new SimpleTimer();
    22     public static SimpleTimer getInstance() { return _instance; }
    23     private final I2PAppContext _context;
     21
     22    /**
     23     *  If you have a context, use context.simpleTimer() instead
     24     */
     25    public static SimpleTimer getInstance() {
     26        return I2PAppContext.getGlobalContext().simpleTimer();
     27    }
     28
    2429    private final Log _log;
    2530    /** event time (Long) to event (TimedEvent) mapping */
    26     private final TreeMap _events;
     31    private final TreeMap<Long, TimedEvent> _events;
    2732    /** event (TimedEvent) to event time (Long) mapping */
    28     private Map _eventTimes;
    29     private final List _readyEvents;
     33    private final Map<TimedEvent, Long> _eventTimes;
     34    private final List<TimedEvent> _readyEvents;
    3035    private SimpleStore runn;
    3136    private static final int MIN_THREADS = 2;
    3237    private static final int MAX_THREADS = 4;
    3338
    34     protected SimpleTimer() { this("SimpleTimer"); }
    35     protected SimpleTimer(String name) {
     39    /**
     40     *  To be instantiated by the context.
     41     *  Others should use context.simpleTimer() instead
     42     */
     43    public SimpleTimer(I2PAppContext context) {
     44        this(context, "SimpleTimer");
     45    }
     46
     47    /**
     48     *  To be instantiated by the context.
     49     *  Others should use context.simpleTimer() instead
     50     */
     51    private SimpleTimer(I2PAppContext context, String name) {
    3652        runn = new SimpleStore(true);
    37         _context = I2PAppContext.getGlobalContext();
    38         _log = _context.logManager().getLog(SimpleTimer.class);
     53        _log = context.logManager().getLog(SimpleTimer.class);
    3954        _events = new TreeMap();
    4055        _eventTimes = new HashMap(256);
     
    4964        int threads = (int) Math.max(MIN_THREADS, Math.min(MAX_THREADS, 1 + (maxMemory / (32*1024*1024))));
    5065        for (int i = 1; i <= threads ; i++) {
    51             I2PThread executor = new I2PThread(new Executor(_context, _log, _readyEvents, runn));
     66            I2PThread executor = new I2PThread(new Executor(context, _log, _readyEvents, runn));
    5267            executor.setName(name + "Executor " + i + '/' + threads);
    5368            executor.setDaemon(true);
    5469            executor.start();
    5570        }
    56         _context.addShutdownTask(new Shutdown());
     71        context.addShutdownTask(new Shutdown());
    5772    }
    5873   
     
    193208    private class SimpleTimerRunner implements Runnable {
    194209        public void run() {
    195             List eventsToFire = new ArrayList(1);
     210            List<TimedEvent> eventsToFire = new ArrayList(1);
    196211            while(runn.getAnswer()) {
    197212                try {
  • core/java/src/net/i2p/util/SimpleTimer2.java

    rdb42a46c rbafef846  
    2727 */
    2828public class SimpleTimer2 {
    29     private static final SimpleTimer2 _instance = new SimpleTimer2();
    30     public static SimpleTimer2 getInstance() { return _instance; }
     29
     30    /**
     31     *  If you have a context, use context.simpleTimer2() instead
     32     */
     33    public static SimpleTimer2 getInstance() {
     34        return I2PAppContext.getGlobalContext().simpleTimer2();
     35    }
     36
    3137    private static final int MIN_THREADS = 2;
    3238    private static final int MAX_THREADS = 4;
    33     private final I2PAppContext _context;
    3439    private final ScheduledThreadPoolExecutor _executor;
    3540    private final String _name;
     
    3742    private final int _threads;
    3843
    39     protected SimpleTimer2() { this("SimpleTimer2"); }
    40     protected SimpleTimer2(String name) {
    41         _context = I2PAppContext.getGlobalContext();
     44    /**
     45     *  To be instantiated by the context.
     46     *  Others should use context.simpleTimer2() instead
     47     */
     48    public SimpleTimer2(I2PAppContext context) {
     49        this(context, "SimpleTimer2");
     50    }
     51
     52    /**
     53     *  To be instantiated by the context.
     54     *  Others should use context.simpleTimer2() instead
     55     */
     56    protected SimpleTimer2(I2PAppContext context, String name) {
     57        this(context, name, true);
     58    }
     59
     60    /**
     61     *  To be instantiated by the context.
     62     *  Others should use context.simpleTimer2() instead
     63     *  @since 0.9
     64     */
     65    protected SimpleTimer2(I2PAppContext context, String name, boolean prestartAllThreads) {
    4266        _name = name;
    4367        _count = 0;
     
    4771        _threads = (int) Math.max(MIN_THREADS, Math.min(MAX_THREADS, 1 + (maxMemory / (32*1024*1024))));
    4872        _executor = new CustomScheduledThreadPoolExecutor(_threads, new CustomThreadFactory());
    49         _executor.prestartAllCoreThreads();
     73        if (prestartAllThreads)
     74            _executor.prestartAllCoreThreads();
    5075        // don't bother saving ref to remove hook if somebody else calls stop
    51         _context.addShutdownTask(new Shutdown());
     76        context.addShutdownTask(new Shutdown());
    5277    }
    5378   
  • history.txt

    rdb42a46c rbafef846  
     12012-03-22 zzz
     2  * Home page: CSS tweaks
     3  * Reseeder: Get rid of static instance, root in netDB,
     4              don't use system properties for status
     5  * Router: When removing a config setting, remove from context also
     6  * SimpleScheduler, SimpleTimer, SimpleTimer2: Replace static instances
     7    with I2PAppContext-rooted references
     8
    192012-03-20 zzz
    210  * i2psnark: Message area tweaks and clear link
  • router/java/src/net/i2p/router/RouterVersion.java

    rdb42a46c rbafef846  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 19;
     21    public final static long BUILD = 20;
    2222
    2323    /** for example "-test" */
Note: See TracChangeset for help on using the changeset viewer.