Changeset 0a521b7


Ignore:
Timestamp:
Mar 17, 2012 9:58:41 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
508533b
Parents:
0c348ec
Message:
  • BuildHandler?: Implement restart and shutdown to stop the thread
Location:
router/java/src/net/i2p/router/tunnel/pool
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/pool/BuildExecutor.java

    r0c348ec r0a521b7  
    4141    /** indexed by ptcc.getReplyMessageId() */
    4242    private final ConcurrentHashMap<Long, PooledTunnelCreatorConfig> _recentlyBuildingMap;
    43     private boolean _isRunning;
     43    private volatile boolean _isRunning;
    4444    private boolean _repoll;
    4545    private static final int MAX_CONCURRENT_BUILDS = 10;
     
    8585    }
    8686   
     87    /**
     88     *  @since 0.9
     89     */
     90    public void restart() {
     91        synchronized (_recentBuildIds) {
     92            _recentBuildIds.clear();
     93        }
     94        _currentlyBuildingMap.clear();
     95        _recentlyBuildingMap.clear();
     96    }
     97
     98    /**
     99     *  Cannot be restarted.
     100     *  @since 0.9
     101     */
     102    public void shutdown() {
     103        _isRunning = false;
     104        restart();
     105    }
     106
    87107    private int allowed() {
    88108        int maxKBps = _context.bandwidthLimiter().getOutboundKBytesPerSecond();
     
    271291        long afterHandleInbound = 0;
    272292               
    273         while (!_manager.isShutdown()){
     293        while (_isRunning && !_manager.isShutdown()){
    274294            //loopBegin = System.currentTimeMillis();
    275295            try {
  • router/java/src/net/i2p/router/tunnel/pool/BuildHandler.java

    r0c348ec r0a521b7  
    5555    private final BuildMessageProcessor _processor;
    5656    private final ParticipatingThrottler _throttler;
    57     private boolean _isRunning;
     57    private volatile boolean _isRunning;
    5858
    5959    /** TODO these may be too high, review and adjust */
     
    123123   
    124124    /**
     125     *  @since 0.9
     126     */
     127    public void restart() {
     128        _inboundBuildMessages.clear();
     129    }
     130
     131    /**
     132     *  Cannot be restarted.
     133     *  @param numThreads the number of threads to be shut down
     134     *  @since 0.9
     135     */
     136    public void shutdown(int numThreads) {
     137        _isRunning = false;
     138        _inboundBuildMessages.clear();
     139        BuildMessageState poison = new BuildMessageState(null, null, null);
     140        for (int i = 0; i < numThreads; i++) {
     141            _inboundBuildMessages.offer(poison);
     142        }
     143    }
     144
     145    /**
    125146     * Thread to handle inbound requests
    126147     * @since 0.8.11
     
    128149    public void run() {
    129150        _isRunning = true;
    130         while (!_manager.isShutdown()) {
     151        while (_isRunning && !_manager.isShutdown()) {
    131152            try {
    132153                handleInboundRequest();
     
    151172                return;
    152173            }
     174
     175            // check for poison
     176            if (state.msg == null) {
     177                _isRunning = false;
     178                return;
     179            }
     180
    153181            long dropBefore = System.currentTimeMillis() - (BuildRequestor.REQUEST_TIMEOUT/4);
    154182            if (state.recvTime <= dropBefore) {
  • router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java

    r0c348ec r0a521b7  
    106106    void shutdown() {
    107107        if (_log.shouldLog(Log.WARN))
    108             _log.warn(toString() + ": Shutdown called", new Exception());
     108            _log.warn(toString() + ": Shutdown called");
    109109        _alive = false;
    110110        _lastSelectionPeriod = 0;
  • router/java/src/net/i2p/router/tunnel/pool/TunnelPoolManager.java

    r0c348ec r0a521b7  
    5151    private final BuildHandler _handler;
    5252    private boolean _isShutdown;
     53    private final int _numHandlerThreads;
    5354    private static final long[] RATES = { 60*1000, 10*60*1000l, 60*60*1000l };
    5455
     
    8182        else
    8283            numHandlerThreads = 1;
    83         numHandlerThreads = ctx.getProperty("router.buildHandlerThreads", numHandlerThreads);
    84         for (int i = 1; i <= numHandlerThreads; i++) {
     84        _numHandlerThreads = ctx.getProperty("router.buildHandlerThreads", numHandlerThreads);
     85        for (int i = 1; i <= _numHandlerThreads; i++) {
    8586            I2PThread hThread = new I2PThread(_handler, "BuildHandler " + i + '/' + numHandlerThreads, true);
    8687            hThread.start();
     
    397398   
    398399    public void restart() {
    399         shutdown();
     400        _handler.restart();
     401        _executor.restart();
     402        shutdownExploratory();
    400403        startup();
    401404    }
     
    549552    }
    550553   
     554    /**
     555     *  Cannot be restarted
     556     */
    551557    public void shutdown() {
     558        _handler.shutdown(_numHandlerThreads);
     559        _executor.shutdown();
     560        shutdownExploratory();
     561        _isShutdown = true;
     562    }
     563
     564    private void shutdownExploratory() {
    552565        if (_inboundExploratory != null)
    553566            _inboundExploratory.shutdown();
    554567        if (_outboundExploratory != null)
    555568            _outboundExploratory.shutdown();
    556         _isShutdown = true;
    557569    }
    558570   
Note: See TracChangeset for help on using the changeset viewer.