In my application I have the MainThread and I have a SeperateThread
I have the threads working almost to perfection. The only problem is I can’t shut the SeperateThread down.
public void run()
{
isRunning = true;
while(isRunning)
{
Log.d(TAG, "Running...");
long currentTime = SystemClock.uptimeMillis();
}
}
public void StopThread()
{
isRunning = false;
}
seperateThread.StopThread();
Then in the Thread I have a method that just turns the volatile boolean isRunning off. Even though I step through in the debugger noting that the thread switches the boolean to off.
- What would cause this type of problem?
- Is this the cleanest way to shut a thread down?
- Any other steps in shutting down a thread?
- Are their any setbacks in Androids multi-threading?
You definitely want to be using the new concurrency package stuff for this, and grab a copy of Java Concurrency in Practice, and learn more about this. Threads are deceptively simple.
Why your code isn’t working probably has to do with you not setting isRunning to false the way you expect. However, the java.util.concurrency package does this for you anyway.
In your case you should look at the Executor framework. So your code could look something like this:
Thinking about this overnight, this might be a better way: