timer.cancel() return 计时器是否为空?
Does timer.cancel() return the timer as null?
我在 java 中有一个 timer
对象。以下是我的代码:
package com.krish.timerproject;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
Button btnStart, btnStop, btnPause;
int x = 0;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = findViewById(R.id.btnStart);
btnPause = findViewById(R.id.btnPause);
btnStop = findViewById(R.id.btnStop);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null){
return;
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.d("testingTimer", String.valueOf(x));
x++;
}
}, 0, 1000);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null){
timer.cancel();
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null){
timer.cancel();
}
x=0;
}
});
}
}
如果我调用 timer.cancel()
它会终止计时器并且 return 它为空,还是计时器对象仍然存在?因此,我必须在 timer.cancel()
之后设置 timer = null;
吗?
我想知道这一点,因为在我点击暂停按钮然后点击开始按钮后,我的计时器没有恢复(它进入 if(timer !=null){return;}
来自 java.util.Timer#cancel
的 Javadoc:
Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.
Note that calling this method from within the run method of a timer task that was invoked by this timer absolutely guarantees that the ongoing task execution is the last task execution that will ever be performed by this timer.
This method may be called repeatedly; the second and subsequent calls have no effect.
如您所见,它取消了计划任务而不中断 运行 个任务。
计时器对象在您执行此操作时会更改其状态,但不会将其自身设置为 null
。
事实上,对象无法修改对自身的引用,因此它无法设置自身 null
,因为 this
无法设置为其他对象,即使您可以改变它的内容,即使它可以,也不会影响对该对象的其他引用。
在您的程序中,您明确必须将 timer
变量设置为 null
。
我在 java 中有一个 timer
对象。以下是我的代码:
package com.krish.timerproject;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
Button btnStart, btnStop, btnPause;
int x = 0;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = findViewById(R.id.btnStart);
btnPause = findViewById(R.id.btnPause);
btnStop = findViewById(R.id.btnStop);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null){
return;
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.d("testingTimer", String.valueOf(x));
x++;
}
}, 0, 1000);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null){
timer.cancel();
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null){
timer.cancel();
}
x=0;
}
});
}
}
如果我调用 timer.cancel()
它会终止计时器并且 return 它为空,还是计时器对象仍然存在?因此,我必须在 timer.cancel()
之后设置 timer = null;
吗?
我想知道这一点,因为在我点击暂停按钮然后点击开始按钮后,我的计时器没有恢复(它进入 if(timer !=null){return;}
来自 java.util.Timer#cancel
的 Javadoc:
Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it. Note that calling this method from within the run method of a timer task that was invoked by this timer absolutely guarantees that the ongoing task execution is the last task execution that will ever be performed by this timer. This method may be called repeatedly; the second and subsequent calls have no effect.
如您所见,它取消了计划任务而不中断 运行 个任务。
计时器对象在您执行此操作时会更改其状态,但不会将其自身设置为 null
。
事实上,对象无法修改对自身的引用,因此它无法设置自身 null
,因为 this
无法设置为其他对象,即使您可以改变它的内容,即使它可以,也不会影响对该对象的其他引用。
在您的程序中,您明确必须将 timer
变量设置为 null
。