Java Timerソース解析(タイマソース解析)

5903 ワード

Timerの概要
Timerは、その名の通りタイマであり、遅延処理が必要なタスクを処理するために使用され、遅延時間は1 sまたは5日である可能性があります.一般的な使用方法は次のとおりです.
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Log.d("test", "timer task test");
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, new Date(), 1000);

初期化
上のコードから見ると、主にTimerというクラスで、まずTimerの初期化を見てみましょう.
    public Timer() {
        this("Timer-" + serialNumber());
    }
    public Timer(String name) {
        thread.setName(name);
        thread.start();
    }
    private final TimerThread thread = new TimerThread(queue);

上のコードから、Timer初期化は実はstartであるTimerThreadというスレッドであることがわかります.そこで、このスレッドの仕事を見てみましょう.
public void run() {
        try {
            mainLoop();
        } finally {
            // Someone killed this Thread, behave as if Timer cancelled
            synchronized(queue) {
                newTasksMayBeScheduled = false;
                queue.clear();  // Eliminate obsolete references
            }
        }
    }
    private void mainLoop() {
        while (true) {
            try {
                TimerTask task;
                boolean taskFired;
                synchronized(queue) {
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && newTasksMayBeScheduled)
                        queue.wait();
                    if (queue.isEmpty())
                        break; // Queue is empty and will forever remain; die

                    // Queue nonempty; look at first evt and do the right thing
                    long currentTime, executionTime;
                    task = queue.getMin();
                    synchronized(task.lock) {
                        if (task.state == TimerTask.CANCELLED) {
                            queue.removeMin();
                            continue;  // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis();
                        executionTime = task.nextExecutionTime;
                        if (taskFired = (executionTime<=currentTime)) {
                            if (task.period == 0) { // Non-repeating, remove
                                queue.removeMin();
                                task.state = TimerTask.EXECUTED;
                            } else { // Repeating task, reschedule
                                queue.rescheduleMin(
                                  task.period<0 ? currentTime   - task.period
                                                : executionTime + task.period);
                            }
                        }
                    }
                    if (!taskFired) // Task hasn't yet fired; wait
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired)  // Task fired; run it, holding no locks
                    task.run();
            } catch(InterruptedException e) {
            }
        }
    }

コードは長くなく、分かりやすい:1、タスクキューが空かどうかを確認し、空であればブロックします.2、キューの中で最も待ち時間の少ないものを取得し、そのステータスが閉じられているかどうか、および繰り返し実行する必要があるかどうかを確認します.3、タスクがキャンセルされていない場合は、キューを削除する必要はありません.では、実行する必要がある時間が現在の時間より小さいかどうかを判断し、等しい場合はwaitを介してその時間まで実行を待機し、大きい場合はrunを直接実行する.
タスクキュー(ルートヒープ)
タイマーは毎回時間の最小のタスクをトリガーし、この極値を取る場合はスタックが適しています.スタック構造を使用すると、主に追加と削除が速く、その性能はlog(n)です.すなわち,1000個のタスクのスタックに追加・削除操作を10回の比較以内に行うことができる.
ソースコードは次のとおりです.
class TaskQueue {

    private TimerTask[] queue = new TimerTask[128];
    private int size = 0;

    int size() {
        return size;
    }

    void add(TimerTask task) {
        // Grow backing store if necessary
        if (size + 1 == queue.length)
            queue = Arrays.copyOf(queue, 2*queue.length);

        queue[++size] = task;
        fixUp(size);
    }

    TimerTask getMin() {
        return queue[1];
    }

    TimerTask get(int i) {
        return queue[i];
    }

    void removeMin() {
        queue[1] = queue[size];
        queue[size--] = null;  // Drop extra reference to prevent memory leak
        fixDown(1);
    }

    void quickRemove(int i) {
        assert i <= size;

        queue[i] = queue[size];
        queue[size--] = null;  // Drop extra ref to prevent memory leak
    }

    void rescheduleMin(long newTime) {
        queue[1].nextExecutionTime = newTime;
        fixDown(1);
    }

    boolean isEmpty() {
        return size==0;
    }

    void clear() {
        // Null out task references to prevent memory leak
        for (int i=1; i<=size; i++)
            queue[i] = null;

        size = 0;
    }

    private void fixUp(int k) {
        while (k > 1) {
            int j = k >> 1;
            if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }

    private void fixDown(int k) {
        int j;
        while ((j = k << 1) <= size && j > 0) {
            if (j < size &&
                queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
                j++; // j indexes smallest kid
            if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }
    void heapify() {
        for (int i = size/2; i >= 1; i--)
            fixDown(i);
    }
}

Timerソースコードにおけるスタック実装は非常に基礎的であり,主に2つの方法,fixUp()とfixDown(),すなわち上昇と沈降である.fixUp:スタック構造を保証するためにaddTaskの時に上昇まで使用します.fixDown:最小値を取り出すと、スタックの最後のノードをスタックトップに配置し、沈下操作を実行し、スタック構造を保証し続け、スタック内の残りの値の最小値をスタックトップに配置します.