⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 poolshrinker.java

📁 RMI英文教程,从各个方面教你怎么进行RMI开发
💻 JAVA
字号:
package com.ora.rmibook.chapter12.pool.finalthread;


public class PoolShrinker implements Runnable {
    private static final int DEFAULT_TIME_TO_WAIT = 120000;
    private static final int DEFAULT_TIME_INTERVAL = 5000;
    private ThreadedPool_Final _owner;
    private int _timeLeftUntilWeShrinkThePool;
    private boolean _paused;
    public PoolShrinker(ThreadedPool_Final owner) {
        _owner = owner;
        _timeLeftUntilWeShrinkThePool = DEFAULT_TIME_TO_WAIT;
    }

    public synchronized void pause() {
        _paused = true;
    }

    public synchronized void resume() {
        _timeLeftUntilWeShrinkThePool = DEFAULT_TIME_TO_WAIT;
        _paused = false;
    }

    public void run() {
        while (true) {
            try {
                Thread.sleep(DEFAULT_TIME_INTERVAL);
            } catch (InterruptedException e) {/* ignored*/
            }
            if (!_paused) {
                decrementClock();
                if (0 == _timeLeftUntilWeShrinkThePool) {
                    _owner.removeAnObject();
                    resetClock();
                }
            }
        }
    }

    private synchronized void resetClock() {
        _timeLeftUntilWeShrinkThePool = DEFAULT_TIME_TO_WAIT;
    }

    private synchronized void decrementClock() {
        _timeLeftUntilWeShrinkThePool -= DEFAULT_TIME_INTERVAL;
        if (0 > _timeLeftUntilWeShrinkThePool) {
            _timeLeftUntilWeShrinkThePool = 0;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -