ZZJ
ZZJ

Reputation: 19

Create a thread in custom view will be leak memory?

public class CustomView extends View{
   public CustomView (Context context) {
        super(context);
    }
  public CustomView (Context context, AttributeSet attrs) {
        super(context, attrs);
        new Thread(){
            @Override
            public void run() {
                while(true){
                   //dosomething; 
                }
            }
        }.start();
    }
}

When the screen config change or other things , it will be leak memory?
If it will be , how should I do ? Thanks.

Upvotes: 1

Views: 116

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

When the screen config change or other things , it will be leak memory?

it will, because of the infinite loop.

how should I do

you have to force your thread to terminate is run method. You could, for instance, setting a boolean that is going be evaluated in the while, and force this boolean onDetachedFromWindow

Upvotes: 1

Related Questions