Should I pause my thread or not Android -


so have thread checks every 10ms's if drag outside draggingzone. basicly thread code doing nothing 99% of time should make pause , resume when needed? or literally nothing when right , left false?

my code looks this

 timer = new thread() { //new thread         public void run() {             b = true;             try {                 {                     sleep(10);                     runonuithread(new runnable() {                          @override                         public void run() {                             if (right) {                                 dragzone.moveleft(-5);                             } else if (left) {                                 dragzone.moveleft(5);                             }                         }                     });                 }                 while (b);             } catch (interruptedexception e) {             }         }          ;     };     timer.start(); 

it looks using thread here not necessary, , should switch using handler , postdelayed()

first, declare handler, boolean, , runnable instance variables:

handler handler; boolean b;  runnable checkdragzone = new runnable(){     @override     public void run() {         if (right) {             dragzone.moveleft(-5);         } else if (left) {             dragzone.moveleft(5);         }          if (b){             handler.postdelayed(this, 10);         }      } }; 

to start monitoring, set b true, , start runnable:

    handler = new handler();     b = true;     handler.postdelayed(checkdragzone, 10); 

to stop (temporarily or permanently), set b false:

    b = false; 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -