0

I heard that making calculations in the GUI thread is a bad idea, so what I've thinked about is this:

float someX;
float someY;
Thread t = new Thread(new Runnable() {

      //making calculations here, setting someX, someY

})

this.runOnUiThread(new Runnable() {

   setX(someX);
   setY(someY);

});

thus they are working parallel AND sharing the same variables inside the class, is this the right way to make things move in the screen?

I've thinked to use the AsyncTask, but is said that is only for short-lived operations...so skiped it

and if the above method is right, why in flash(actionscript) all the calculations are made in the onFrame event(which I think the GUI thread)?

addEventListener(Event.ENTER_FRAME, function(){

//all is made here

});

of course I know that actionscript is one threaded, just asking

any suggestions or advices please

1 Answers1

1

This really depends on how much work you are doing. Most times, I can use the main thread for small graphical calculations. Im not sure what you are doing, but something like setting a position based on an input sounds like a main thread thing. Generally, if you expect an instant response, you should be able to put it in the main thread.

You can tell if you need to move the work to a new thread because your app will start lagging. At that point, you should work in a new thread. This might be helpful.

Community
  • 1
  • 1
Eric S.
  • 1,502
  • 13
  • 31
  • in that answer is said that: There is AsyncTask in android which enables doing long time processes on the UI thread, but in the docs I read that is for short-lived operations, can't understand where is the truth :D – Человек и PHP Mar 12 '15 at 17:46
  • Its definitely the former. AsyncTask is a way to run stuff in a new thread in Android and have the result return to the main thread. I use AsyncTask for network stuff, when I don't know when I will return. – Eric S. Mar 12 '15 at 17:48