1

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???

nynohu
  • 1,628
  • 12
  • 12
java
  • 1,165
  • 1
  • 25
  • 50

1 Answers1

2

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);
} 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • good solution, but do you think there is a performance bottleneck in my code? _I could draw the text on a Canvas but using xml-layout is much simpler_ – java Dec 22 '16 at 06:05
  • 1
    I don't know - I don't have your code. It is up to you to watch memory usage or UI lag – OneCricketeer Dec 22 '16 at 06:06