06-25-2011 04:26 AM
Hi ,
I am developing database based project. Here Three tab buttons are there. each button represent each screen.
when i am navigating from one screen to another screen using tab button,then automatically one thread created.
but i am not using any threads here. in this process means when ever i click ant tab it create one new thread with same name.
each screen starting i am monitiring thread name and number using
" System.out.println(Thread.currentThread().getName
here i ubserve that thread name is same but number of threads increasing.
when the number reach 64, after the project Freezing( Too many threads error). at the time i need to reboot the mobile.
here i want to now that how to kill the active background threads?
-----------please help me, i am strucking here
06-27-2011 01:11 AM
06-27-2011 01:48 AM - edited 06-27-2011 01:57 AM
I hope I am understanding you correctly. I think you want a way to get a list of all the background threads for a process and a method to kill them. I don't know of any way to do that so I am going to present a way that involves keeping track of all the threads your application creates, then call a kill command on them.
To kill a thread I believe all that needs to occur is for the run() method of the thread/runnable argument to complete. You could pass a kill switch that brings the thread/runnable to the end of its execution. Keep track of all the active threads/runnables you create then just kill them when you need to.
Psuedo code:
Runnable runnable1 = new Runnable()
{
private boolean killActivated=false;
public void run() {
while (kill()==false) {
// do stuff
}
}
private boolean kill() {
return killActivated;
}
public void setKill() {
killActivated=true;
}
}
When it is time to kill the thread, call "runnable1.setKill();". Keep track of all of the threads/runnables your application creates in an array or whatever manner you choose.
Scott
06-28-2011 03:39 AM
thank u for ur replay dear
Runnable runnable1 = new Runnable()
{
private boolean killActivated=false;
public void run() {
while (kill()==false) {
// do stuff
}
}
private boolean kill() {
return killActivated;
}
public void setKill() {
killActivated=true;
}
};
you r giving this code but here how i invoke setkill() method by using runnable1 object
plz help me
06-28-2011 03:42 AM
I want a way to get a list of all the background threads for a process and a method to kill them.
06-28-2011 03:43 AM
HI
As such there is no facility available to get the threads and also you have to implement your own logic to close a thread.
Ankit
06-28-2011 06:20 AM
please use Thread.activeCount(), its gives exact no of threads running at the time.
06-28-2011 07:13 AM
thank u dear,
could u plz tell me, when i am goining to one screen to another screen ,automatically thread created.
y it is created?
06-28-2011 07:18 AM
"goining to one screen to another screen ,automatically thread created.y it is created?"
If you use pushModal, this will use a Thread. Try not to use pushModal.
Also can I ask you to please spell check and use proper English on these posts. Remember that some of our readers do not use English as their first language and will not understand abbreviations and spelling mistakes. Thanks
06-28-2011 08:45 AM
If your problem has been solved, please mark problem as solved, and post new query in another thread.