如何在使用多线程时避免重新创建线程



我的主要目的是:每10分钟,应用程序同步服务器上的时间。然后,时钟将从从服务器获取的时间开始计时,而倒计时时钟将显示到下一次同步的剩余时间。

我的代码如下。似乎线程每10分钟重新创建一次,所以时钟显示错误。但是,我不知道如何修复它。请帮帮我。谢谢!

public class MainActivity extends Activity {
    private AnalogClockView analogClockView;
    private FrameLayout analogLayout;
    private long now;
    private ClockAsyncTask mClockAsyncTask;
    private CountDownTimer mCountDownTimer = new CountDownTimer(10*60*1000, 1000) {
        @Override 
        public void onFinish() {
            mClockAsyncTask = (ClockAsyncTask) new ClockAsyncTask();
            mClockAsyncTask.execute();                  
        }
        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub  
            analogClockView.updateTime();
            analogClockView.postInvalidate();
        }
      };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
        analogLayout = (FrameLayout) findViewById(R.id.analogLayout); 
        mCountDownTimer.start();        
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private class ClockAsyncTask extends AsyncTask<Object, Object, Object> {            
        @Override
        protected Object doInBackground(Object... arg0) {
            getNTPTime();
            return null;
        }
        @Override
        protected void onPostExecute(Object result) {
            analogClockView = new AnalogClockView(MainActivity.this, now, analogLayout.getWidth()/2, analogLayout.getHeight()/2);
            analogLayout.removeAllViews();
            analogLayout.addView(analogClockView);
        }
    }
     public void getNTPTime(){
        SntpClient client = new SntpClient();
        if (client.requestTime("0.ubuntu.pool.ntp.org", 1000)) {
            now = client.getNtpTime() + SystemClock.elapsedRealtime()
                    - client.getNtpTimeReference();
            Calendar current = Calendar.getInstance();
            current.setTimeInMillis(now);               
        }
     }   
}

问题是代码将相同的 Runnable用于线程返回到postDelayed没有结束。线程只运行很短的时间,但是代码会持续发布消息(并且每个消息都会发布另一个消息(每个消息都会发布…))。

一个解决方案是使用一个变量来决定何时发布另一个事件。
// Not really a "Thread"
public class CountdownRunner implements Runnable {
    public void run() {
        if (countdownRunning) {
            MainActivity.this.updateHandler.sendEmptyMessage(0);
            // STOP calling when then the countdown is over
            // The fundamental problem is this series of delayed postbacks
            // is never sopped in the original code.
            updateHandler.postDelayed(this, 1000);
        }
    } 
    public void cancel() {
       // ..
    }
}

同时,抛弃创建新线程,因为显式线程在这里没有任何用途:提供的Runnable的run方法只在线程上调用一次,然后线程死亡。(run方法的后续调用是在UI线程上响应postDelayed回调。)

private CountdownRunner countdown;
protected void onPostExecute(Object result) {
    // Start the countdown with the time from the server,
    // cancelling the previous countdown as required.
    if (countdown != null) {
        countdown.cancel();
    }
    // No "new Thread"! this is running on the UI thread and all
    // subsequent executions of run for this instance will also be
    // on the UI thread in the postDelayed postback.
    countdown = new CountdownRunner();
    countdown.run();
}

虽然上面的解释了问题,但我建议简单地使用CountDownTimer,因为它很好地封装了逻辑:

[A CountDownTimer计划]倒计时,直到未来的某个时间,并在沿途的间隔中定期通知。

那么流一般是这样的:

  • 创建CountDownTimer,用每个onTick的剩余时间更新UI。当倒计时结束并且onFinish事件触发时,创建一个新的AsyncTask。

  • 让AsyncTask(即ClockAsyncTask)在doInBackground方法中下载时间。在onPostExecute方法中,新建CountDownTimer

  • 无限重复这个循环;确保适当取消CountDownTimers

最新更新