码迷,mamicode.com
首页 > 其他好文 > 详细

单测中会用到的类,锁+定时器,等待回调的值返回

时间:2015-03-19 16:42:50      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

    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;
            }
        }
    }

单测中会用到的类,锁+定时器,等待回调的值返回

标签:

原文地址:http://my.oschina.net/u/435726/blog/388995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!