-1

In Android, is it necessary to close or stop all the running background threads before the app closes? If yes then how to do that?

I have multiple thread in my program. Say I am running some xyz operation on the first background thread. The problem that I am facing is that even after some time xyz operation on first background is still running, so I thought of stopping that thread explicitly.

APC
  • 144,005
  • 19
  • 170
  • 281
Kirthan
  • 11
  • 5

3 Answers3

1

is it necessary to close/stop all the background thread

It depends on what do you need.

how to do that?

You use TimerTask (link how to cancel), AsyncTask (link how to cancel), Thread (link how to cancel), Runnable (link how to cancel) ?

Community
  • 1
  • 1
Vladimir Markeev
  • 654
  • 10
  • 25
  • thing is I am running some xyz operation on say first background thread( i have multiple thread in my program ) problem that I am facing is tat even after some time xyz operation on first background keeps running, so thought of closing that thread after some thing ?? – Kirthan Nov 17 '15 at 06:21
  • you may close operation if task was completed or you do not need it any more – Vladimir Markeev Nov 17 '15 at 06:56
0

See @kirthan,

Your application always working on main-thread, sub threads of your application are mostly close after them work completed automatically instead of some special case.

but suppose you want to terminate between the working thread follow vmarkeev answer.

Community
  • 1
  • 1
Vishal Patel
  • 2,931
  • 3
  • 24
  • 60
  • Thank u bro !! then in java why is it necessary to close the thread ?? – Kirthan Nov 17 '15 at 06:57
  • see android basically work on core java, actually same concepts. but in some of case you must close thread. all object oriented thread even machine thread also stop automatically after completed task. that's behavior of thread – Vishal Patel Nov 17 '15 at 07:07
  • for example one car is running on road while petrol is there. as like thread is running while the task is there. – Vishal Patel Nov 17 '15 at 07:09
-2

You can use interrupt() to stop any thread

user3037028
  • 76
  • 2
  • 6
  • Which context do you mean? Application context, component context, process context? In either case, the answer is wrong, or in best case incomplete. – Robert Nov 17 '15 at 06:22
  • Your edited answer is completely different from your first answer. Edits should be used to refine an answer, not rewrite it. Oh, and don't be put down by the negative remarks you get here. Just give it some thought and I'm sure that your next answer will get positive response. – Robert Nov 18 '15 at 05:18