單測中會用到的類,鎖+定時器,等待回調的值返回

public class BaseJunit {
    private Timer timer;
    private Object mSync = new Object();

    protected void Wait() {
        synchronized (mSync) {
            if (timer != null) {
                timer.cancel();
                timer.purge();
                timer = null;
            }
            timer = new Timer();
            timer.schedule(task, 3 * 1000);
        }
        synchronized (mSync) {
            try {
                mSync.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    protected TimerTask task = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Notify();
        }

    };

    protected void Notify() {
        synchronized (mSync) {
            mSync.notifyAll();
        }
    }

    protected void removeTimerTask() {
        synchronized (mSync) {
            if (timer != null) {
                timer.cancel();
                timer.purge();
                timer = null;
            }
        }
    }
相關文章
相關標籤/搜索