如何使用 ScheduledExecutorService 更改重复任务的速率或周期?
How do I change the rate or period of a repeating task using ScheduledExecutorService?
我有蓝牙聊天示例应用程序的修改版本。我已经设置了一个 ScheduledExecutorService
,它使用 scheduleAtFixedRate
.
以预定义的速率通过蓝牙发送命令
我设置了一个PreferenceActivity
允许用户修改时间段。但我不确定如何让实际的重复任务在更新期间发生。我是否需要以某种方式取消并重新启动 ScheduledExecutorService
?
这是我的代码的相关部分。
private ScheduledExecutorService scheduleTaskExecutor;
public long ReadInterval = 1;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
...
// This schedule a task to run every 1 second:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
// If you need update UI, simply do this:
runOnUiThread(new Runnable() {
public void run() {
// update your UI component here.
if (connected == true) {
sendMessage("READ");
if (D) Log.i(TAG, "In Run!");
}
}
});
}
}, 0, ReadInterval, TimeUnit.SECONDS);
}
我试图在这里更新 ReadInterval
。 ReadInterval
正在更新,但循环命令周期未更新。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (D)
Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
...
case REQUEST_ENABLE_BT:
...
case REQUEST_SETTINGS:
// When returning from settings activity
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String Pref = sharedPref.getString(SettingsActivity.KEY_PREF_READINTERVAL, "");
ReadInterval = Long.valueOf(Pref);
Toast.makeText(this, Pref,
Toast.LENGTH_SHORT).show();
Log.d(TAG, "Settings Activity Result");
}
}
我稍微改进了答案(主要是因为我也不得不使用 ScheduledExecutorService
)。请使用此代码而不是我发布的上一个代码,因为上一个代码确实是关于性能的,没有充分的理由。
private ScheduledExecutorService scheduledExecutorService;
private ScheduledFuture<?> futureTask;
private Runnable myTask;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Your executor, you should instanciate it once for all
scheduledExecutorService = Executors.newScheduledThreadPool(5);
// Since your task won't change during runtime, you can instanciate it too once for all
myTask = new Runnable()
{
@Override
public void run()
{
// Your code to run periodically
}
};
}
/**
* This method will reschedule "myTask" with the new param time
*/
public void changeReadInterval(long time)
{
if(time > 0)
{
if (futureTask != null)
{
futureTask.cancel(true);
}
futureTask = scheduledExecutorService.scheduleAtFixedRate(myTask, 0, time, TimeUnit.SECONDS);
}
}
现在,要在运行时重新安排任务,请使用方法 changeReadInterval(time);
之前的"timer"已经设置的会取消,重新安排新的。
我有蓝牙聊天示例应用程序的修改版本。我已经设置了一个 ScheduledExecutorService
,它使用 scheduleAtFixedRate
.
我设置了一个PreferenceActivity
允许用户修改时间段。但我不确定如何让实际的重复任务在更新期间发生。我是否需要以某种方式取消并重新启动 ScheduledExecutorService
?
这是我的代码的相关部分。
private ScheduledExecutorService scheduleTaskExecutor;
public long ReadInterval = 1;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
...
// This schedule a task to run every 1 second:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
// If you need update UI, simply do this:
runOnUiThread(new Runnable() {
public void run() {
// update your UI component here.
if (connected == true) {
sendMessage("READ");
if (D) Log.i(TAG, "In Run!");
}
}
});
}
}, 0, ReadInterval, TimeUnit.SECONDS);
}
我试图在这里更新 ReadInterval
。 ReadInterval
正在更新,但循环命令周期未更新。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (D)
Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
...
case REQUEST_ENABLE_BT:
...
case REQUEST_SETTINGS:
// When returning from settings activity
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String Pref = sharedPref.getString(SettingsActivity.KEY_PREF_READINTERVAL, "");
ReadInterval = Long.valueOf(Pref);
Toast.makeText(this, Pref,
Toast.LENGTH_SHORT).show();
Log.d(TAG, "Settings Activity Result");
}
}
我稍微改进了答案(主要是因为我也不得不使用 ScheduledExecutorService
)。请使用此代码而不是我发布的上一个代码,因为上一个代码确实是关于性能的,没有充分的理由。
private ScheduledExecutorService scheduledExecutorService;
private ScheduledFuture<?> futureTask;
private Runnable myTask;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Your executor, you should instanciate it once for all
scheduledExecutorService = Executors.newScheduledThreadPool(5);
// Since your task won't change during runtime, you can instanciate it too once for all
myTask = new Runnable()
{
@Override
public void run()
{
// Your code to run periodically
}
};
}
/**
* This method will reschedule "myTask" with the new param time
*/
public void changeReadInterval(long time)
{
if(time > 0)
{
if (futureTask != null)
{
futureTask.cancel(true);
}
futureTask = scheduledExecutorService.scheduleAtFixedRate(myTask, 0, time, TimeUnit.SECONDS);
}
}
现在,要在运行时重新安排任务,请使用方法 changeReadInterval(time);
之前的"timer"已经设置的会取消,重新安排新的。