Android Open Source - SurgeTracker Timer Thread






From Project

Back to project page SurgeTracker.

License

The source code is released under:

MIT License

If you think the Android project SurgeTracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bklimt.surgetracker;
//from  w  w  w. j a va2  s .  c  o m
import android.os.Handler;
import android.os.Looper;

import java.util.ArrayList;

public class TimerThread {
    private static Object lock = new Object();
    private static boolean running = false;

    private static ArrayList<Runnable> minuteListeners = new ArrayList<Runnable>();
    private static ArrayList<Runnable> secondListeners = new ArrayList<Runnable>();
    private static Handler handler = new Handler(Looper.getMainLooper());

    private static void ensureThreadRunning() {
        synchronized (lock) {
            if (!running) {
                running = true;
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int seconds = 0;
                        while (true) {
                            try {
                                // Should run at least every second.
                                Thread.sleep(500);

                                ArrayList<Runnable> secondListenersCopy;
                                synchronized (lock) {
                                    secondListenersCopy = new ArrayList<Runnable>(secondListeners);
                                }
                                for (Runnable listener : secondListenersCopy) {
                                    handler.post(listener);
                                }

                                if (++seconds < 60) {
                                    continue;
                                }
                                seconds = 0;

                                ArrayList<Runnable> minuteListenersCopy;
                                synchronized (lock) {
                                    minuteListenersCopy = new ArrayList<Runnable>(minuteListeners);
                                }
                                for (Runnable listener : minuteListenersCopy) {
                                    handler.post(listener);
                                }
                            } catch (InterruptedException e) {
                                // Ignore it. There's really no way to kill this thread.
                            }
                        }
                    }
                });
                thread.setName(TimerThread.class.getName());
                thread.start();
            }
        }
    }

    public static void atLeastEveryMinute(Runnable runnable) {
        synchronized (lock) {
            minuteListeners.add(runnable);
            ensureThreadRunning();
        }
    }

    public static void atLeastEverySecond(Runnable runnable) {
        synchronized (lock) {
            secondListeners.add(runnable);
            ensureThreadRunning();
        }
    }

    public static void removeListener(Runnable runnable) {
        synchronized (lock) {
            minuteListeners.remove(runnable);
            secondListeners.remove(runnable);
        }
    }
}




Java Source Code List

com.bklimt.surgetracker.ApplicationTest.java
com.bklimt.surgetracker.SurgeTrackerApplication.java
com.bklimt.surgetracker.TimerThread.java
com.bklimt.surgetracker.backbone.CollectionListener.java
com.bklimt.surgetracker.backbone.Collection.java
com.bklimt.surgetracker.backbone.ModelListener.java
com.bklimt.surgetracker.backbone.Model.java
com.bklimt.surgetracker.backbone.Visitor.java
com.bklimt.surgetracker.model.AggregateCollection.java
com.bklimt.surgetracker.model.Aggregate.java
com.bklimt.surgetracker.model.RootViewModel.java
com.bklimt.surgetracker.model.SurgeCollection.java
com.bklimt.surgetracker.model.SurgeParseObject.java
com.bklimt.surgetracker.model.Surge.java
com.bklimt.surgetracker.view.AggregateListAdapter.java
com.bklimt.surgetracker.view.AggregateListFragment.java
com.bklimt.surgetracker.view.MainActivity.java
com.bklimt.surgetracker.view.SurgeDeletionDialogFragment.java
com.bklimt.surgetracker.view.SurgeDurationDialogFragment.java
com.bklimt.surgetracker.view.SurgeGraphFragment.java
com.bklimt.surgetracker.view.SurgeListAdapter.java
com.bklimt.surgetracker.view.SurgeListFragment.java
com.bklimt.surgetracker.view.SurgeStartDialogFragment.java