短时间间隔重复方法
Repeat method in short time interval
我有一个 activity,我必须每秒更新视图(如图像)。我尝试了几种不同的方法,发现它们可以实现相同的功能。但是哪种方法最有效并减少内存泄漏?
以下是不同的方法 -
方法一
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//DO SOMETHING
handler.postDelayed(this, 1000);
}
}, 1000);
方法二
ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
//DO SOMETHING
}
}, 0, 1, TimeUnit.SECONDS);
方法三
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
//DO SOMETHING
}
}, 0, 1000);
我应该使用哪一个?我问这个问题是因为我在很短的时间内更新了我的 GUI。
上下文
考虑到 Android 使用它自己的 java 实现这一事实。
There is no Java Virtual Machine in the Android platform. Java
bytecode is not executed. Instead Java classes are compiled into a
proprietary bytecode format and run on Dalvik, a specialized virtual
machine (VM) designed specifically for Android. Unlike Java VMs, which
are stack machines, the Dalvik VM is a register-based architecture.
Comparison of Java and Android API
因此,适用于 java 虚拟机的内容可能不一定反映在 Android 的实现中。
根据经验,最好使用 Android API 提供的任何内容。
解决方案
我推荐使用 Handler。
特别是如果你想更新视图。
甚至 official Android tutorials 推荐:
To move data from a background thread to the UI thread, use a Handler
that's running on the UI thread.
从一开始计时器被排除在外。
There are some disadvantages of using Timer It creates only single
thread to execute the tasks and if a task takes too long to run, other
tasks suffer. It does not handle exceptions thrown by tasks and thread
just terminates, which affects other scheduled tasks and they are
never run
来自 this answer。
并且来自 Android docs:
A ThreadPoolExecutor (i.e. ScheduledThreadPoolExecutor) is preferable to Timer when multiple worker threads are needed, or when
the additional flexibility or capabilities of ThreadPoolExecutor
(which this class extends) are required.
使用处理程序时如果您想避免内存泄漏,我建议通读this tutorial。
如果不是 UI 更新 ThreadPoolExecutor 会更好 because 它们在执行大量异步任务时提供改进的性能,因为减少了每个任务的调用开销。
如果您想更新屏幕上的值或图像,您应该使用第一种方法。我用第一种方法解决了同样的问题。
方法一
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//DO SOMETHING
handler.postDelayed(this, 1000);
}
}, 1000);
备选
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//update here (images or other values)
}
});
}
} catch (InterruptedException e) {
}
}
};
我有一个 activity,我必须每秒更新视图(如图像)。我尝试了几种不同的方法,发现它们可以实现相同的功能。但是哪种方法最有效并减少内存泄漏?
以下是不同的方法 -
方法一
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//DO SOMETHING
handler.postDelayed(this, 1000);
}
}, 1000);
方法二
ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
//DO SOMETHING
}
}, 0, 1, TimeUnit.SECONDS);
方法三
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
//DO SOMETHING
}
}, 0, 1000);
我应该使用哪一个?我问这个问题是因为我在很短的时间内更新了我的 GUI。
上下文
考虑到 Android 使用它自己的 java 实现这一事实。
There is no Java Virtual Machine in the Android platform. Java bytecode is not executed. Instead Java classes are compiled into a proprietary bytecode format and run on Dalvik, a specialized virtual machine (VM) designed specifically for Android. Unlike Java VMs, which are stack machines, the Dalvik VM is a register-based architecture.
Comparison of Java and Android API
因此,适用于 java 虚拟机的内容可能不一定反映在 Android 的实现中。
根据经验,最好使用 Android API 提供的任何内容。
解决方案
我推荐使用 Handler。 特别是如果你想更新视图。
甚至 official Android tutorials 推荐:
To move data from a background thread to the UI thread, use a Handler that's running on the UI thread.
从一开始计时器被排除在外。
There are some disadvantages of using Timer It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run
来自 this answer。
并且来自 Android docs:
A ThreadPoolExecutor (i.e. ScheduledThreadPoolExecutor) is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.
使用处理程序时如果您想避免内存泄漏,我建议通读this tutorial。
如果不是 UI 更新 ThreadPoolExecutor 会更好 because 它们在执行大量异步任务时提供改进的性能,因为减少了每个任务的调用开销。
如果您想更新屏幕上的值或图像,您应该使用第一种方法。我用第一种方法解决了同样的问题。
方法一
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//DO SOMETHING
handler.postDelayed(this, 1000);
}
}, 1000);
备选
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//update here (images or other values)
}
});
}
} catch (InterruptedException e) {
}
}
};