Note that your code is not synchronized and may cause problems.

Your stopThread method sets blinker to null and stopThread is probably
called by the UI thread.

The 'run' method runs in the other thread and reads the value of
'blinker'.

Since you haven't synchronized the writing and reading of 'blinker'
you may get into race conditions.

In your particular example, you probably won't get into trouble
(depening on what 'work()' does), but you should synchronize the
access to 'blinker'.

On Mar 4, 5:48 pm, Joshua Frank <[email protected]> wrote:
> Thank you so much! You led me right to the problem. I was using code I
> found on Sun's website on how to start and stop threads and it was
> creating a second thread every time it ran.
>
> I want to post the solution in case anyone else has this issue.
>
> This is what I started with.
> blinker makes a new thread and that was the issue.
>
> public class AndroidThread extends Thread {
>
>         private Thread blinker;
>
>         @Override
>         public void run() {
>                 Thread thisThread = Thread.currentThread();
>                 while(blinker == thisThread){
>                         work();
>                 }
>
>         }
>
>         public void work() {
>
>         }
>
>         public void startThread() {
>                 blinker = new Thread(this);
>                 blinker.start();
>         }
>
>         public void stopThread() {
>                 blinker = null;
>         }
>
> }
>
> This is what I changed it to.
>
> public class AndroidThread extends Thread {
>
>         private Thread blinker;
>
>         @Override
>         public void run() {
>                 Thread thisThread = Thread.currentThread();
>                 while(blinker == thisThread){
>                         work();
>                 }
>
>         }
>
>         public void work() {
>
>         }
>
>         public void startThread() {
>                 blinker = this;
>                 blinker.start();
>         }
>
>         public void stopThread() {
>                 blinker = null;
>         }
>
>
>
>
>
> }
> On Thu, Mar 4, 2010 at 3:36 PM, fadden <[email protected]> wrote:
> > On Mar 4, 1:02 pm, Joshua Frank <[email protected]> wrote:
> >> I have a couple of worker threads in my app which I stop and restart
> >> during onPause() and onResume(). I use a method that clears all the
> >> reference to and from the threads when stopping. However ThreadGroup
> >> is still retaining a reference to my threads and causing a small
> >> memory leak. After pausing and resuming my app about 40 to 50 times I
> >> will then get OutOfMemory Errors when loading bitmaps. Allthough this
> >> doesn't effect most of my users I do get occasional reports of
> >> crashes.
>
> > The only leak I know of in this area happens with Threads that are
> > never actually started.  Threads remove themselves from the
> > ThreadGroup before they exit, but if they're never started then they
> > never have a chance to do the remove.
>
> > See also:
> >  http://groups.google.com/group/android-beginners/browse_thread/thread...
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Joshua Frank- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to