更改变量后,是否必须重新启动?

时间:2016-02-06 00:25:21

标签: java android string multithreading performance

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

first_hash

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

second_hash

既然我已经改变了我的共享偏好,我是否需要重新启动由两个线程组成的服务?我需要对线程做任何事吗?这有效吗?

非常感谢,

Ruchir

1 个答案:

答案 0 :(得分:0)

嗯,这样做会使泄漏方式过多,你的Service类在一个Thread上运行,如果你调用//C# string path = @"c:\temp\MyNumber.txt"; if (!File.Exists(path)) { // Create a file to write to. string number = "4.243523"; File.WriteAllText(path, number); } //ANSI C char line[80]; char *dummy; FILE *fp = fopen("c:\\temp\\MyNumber.txt", "r"); if(fp) { if(fgets(line, 80, fp) { double num = strtod(line, &dummy); } ... 并且让我说他是唯一的运行保持MainThread活着然后它将停止离开这两个{ {1}}可以玩。

线程不依赖于服务,因此您所做的就是服务类中有stopSelf() Thread,如此

Volatile

当你想重启你的toggle killThreads = true; ,现在在你的Threads工作负载的实现中,你需要做的是,如果它们处于循环中,他们需要始终检查killThreads标志,如果它的真实死亡时间如果没有继续,如果不是循环,仍然需要在每个主要代码行之后不断检查该标志

示例

boolean

在具体代码行

之后检查标志

现在在你的活动中

 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

希望有所帮助