new Runnable() but no new thread?

前端 未结 8 2122
名媛妹妹
名媛妹妹 2020-12-08 11:11

I\'m trying to understand the code here , specifically the anonymous class

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
   final l         


        
8条回答
  •  时光说笑
    2020-12-08 11:49

    The Runnable interface is another way in which you can implement multi-threading other than extending the Thread class due to the fact that Java allows you to extend only one class.

    You can however, use the new Thread(Runnable runnable) constructor, something like this:

    private Thread thread = new Thread(new Runnable() {
    public void run() {
       final long start = mStartTime;
       long millis = SystemClock.uptimeMillis() - start;
       int seconds = (int) (millis / 1000);
       int minutes = seconds / 60;
       seconds     = seconds % 60;
    
       if (seconds < 10) {
           mTimeLabel.setText("" + minutes + ":0" + seconds);
       } else {
           mTimeLabel.setText("" + minutes + ":" + seconds);            
       }
    
       mHandler.postAtTime(this,
               start + (((minutes * 60) + seconds + 1) * 1000));
       }
    });
    
    thread.start();
    

提交回复
热议问题