Make anonymous class static - through static method

538 views Asked by At

I have thoughts about the following design

public void cntDownPossesion() {

  ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {

                int poss = Integer.parseInt(possesionTextView.getText().toString());
                poss--;
                possesionTextView.setText("" + poss);
                iCount++;
            }

} 

Since a new Runnable() is created every frame. I use a SurfaceView and it uses a workerthread. The textview is created on the mainthread so I have to put the updates of "poss" back to the mainthread since the workerthread cannot touch it.

I guess this may create high memory consumtion and do not know if the GC keeps up??

I came with the Idea to make the Runnable-object static but since its an innerclass this is not possible. What about making the context, that is the method cntDownPossesion static - if the method is static, isnt the Innerclass itself static then???

1

There are 1 answers

2
OneCricketeer On BEST ANSWER

You can extract the Runnable to a member variable if you really think it is a performance bottleneck.

private Context context;
private TextView possesionTextView;
private int iCount;

private final Runnable r = new Runnable() {
    @Override
    public void run() {
        int poss = -1 + Integer.parseInt(possesionTextView.getText().toString());
        possesionTextView.setText(String.valueOf(poss));
        iCount++;
    }
};

public void cntDownPossesion() {
  ((Activity) context).runOnUiThread(r);
}