更改变量后,是否必须重新启动? [英] After changing a variable, do I have to restart?

查看:86
本文介绍了更改变量后,是否必须重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长期运行的服务,它由两个线程组成.

I have a long running service, that consists of two threads.

//MY SERVICE:

if(userEnabledProcessOne) //Shared preference value that I'm getting from the settings of my app based on a checkbox

    processOneThread.start();

if(userEnabledProcessTwo)

    processTwoThread.start();

基本上,我为用户提供了一个复选框来启用/禁用这些进程的选项. 现在,如果用户决定在服务运行时禁用其中一个进程,是否需要使用更新的共享首选项重新启动服务?这样吗?

Basically, I give the user the option to enable/disable these processes with a checkbox. Now, if the user decides to disable one of the processes while the service is running, do I need to restart my service with the updated shared preferences? Like so?

//In the settings activity of my app
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // Change shared preferences to make the process enabled

 stopService(myService.class)
 startService(myService.class)

        } if (!isChecked)
            // Change shared preferences to make the process enabled


 stopService(myService.class)
 startService(myService.class)
    }

现在,我已经更改了共享首选项,是否需要重新启动由两个线程组成的服务?我需要对线程做任何事情吗?这样有效吗?

Now that I've changed my shared preferences, do I need to relaunch my service consisting of two threads? Do I need to do anything to the threads? Is this efficient?

非常感谢

Ruchir

推荐答案

嗯,这样做会泄漏太多泄漏,如果调用stopSelf()并说他是唯一的运行方式,则Service类在线程上运行保持MainThread处于活动状态,然后它将停止留下两个Thread来玩耍.

Hmm, doing that will leeak leak way too much, your Service class runs on a Thread, if you call stopSelf() and lets say he is the only run keep the MainThread alive then it will stop leaving the two Threads to play around.

线程不依赖于服务,因此您要做的是像这样在服务类中有一个Volatile boolean

Threads do not depend on the Service, so what you do is you have a Volatile boolean in your service class like so

 private volatile boolean killThreads = false;//guess in your case this
 //flag will be binded to the isChecked of the checkbox or even the 
 //interface method

当您要重新启动时,可以切换killThreads = true; ,现在在执行Threads工作负载时,您需要做的是如果它们处于循环中,则需要始终检查killThreads标志(如果不继续,则为真,它的死亡时间);如果不在循环中,则仍然需要在每个主要代码行之后不断检查该标志

when you want to restart the you toggle killThreads = true; , and now in your implementation of your Threads workloads, what you need to do is if they are in a loop they need to always check killThreads flag if its true its death time if not continue, if not in a loop, still you need to constantly be checking for that flag after every major code line

示例

//in your Service class
private volatile boolean killThreads = false;
//we have jumped to your thread - i am creating a new instance
while(!killThreads){ //if means if this flag is false keep your loop
//hard code comes here
}
//or if you are not running a loop
//in your run method you can constantly check for this flag like
for(int i =0; i < 100; i++){
   if(killThreads){
      break;//or return if you like
     }
    //rest of the code
 }
 //after the loop you can also check again
 if(killThreads){
    return;
  }

只需在具体代码行之后检查标志

just check the flag after a concrete code lines

现在参加您的活动

  //In the settings activity of my app
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
    // Change shared preferences to make the process enabled
   //what you do here is you update killThreads with the isChecked
   //so find a way to bind your bind your Activity to Service class
  //there are tons of example on here

希望有帮助

这篇关于更改变量后,是否必须重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆