java - Thread seems to magically start -


i have issue thread while working android studio. here thread class :

class mainloop extends thread {       private boolean run = true;      @override     public void run() {         system.out.println("thread started");         run = true;          final double spf = 1.0/2.0;         double deltas;         long lasttime = system.nanotime();         long newtime;          while(run) {             newtime = system.nanotime();             deltas = (float)(newtime-lasttime)/1000000000f;             if (deltas > spf) {                 lasttime = newtime;                 system.out.println("hello thread");             }         }         system.out.println("thread killed");     }      void kill() {         system.out.println("killing thread");         run = false;     }  } 

and here mainactivity class :

    public class mainactivity extends appcompatactivity {           private mainloop mainloop = null;          @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             mainloop = new mainloop();             mainloop.start();         }          @override         protected void onresume() {             super.onresume();             mainloop = new mainloop();             mainloop.start();         }          @override         public boolean ontouchevent(motionevent event) {             mainloop.kill();             return false;         }     } 

and here output have :

08-15 12:24:07.480 2581-2667/com.lteii.hello i/system.out: hello thread 08-15 12:24:07.593 2581-2671/com.lteii.hello i/system.out: hello thread 08-15 12:24:07.595 2581-2581/com.lteii.hello d/viewrootimpl@e1e91f8[mainactivity]: viewpostimeinputstage processpointer 0 08-15 12:24:07.596 2581-2581/com.lteii.hello w/system: classloader referenced unknown path: /system/framework/qperformance.jar 08-15 12:24:07.597 2581-2581/com.lteii.hello e/boostframework: boostframework() : exception_1 = java.lang.classnotfoundexception: didn't find class "com.qualcomm.qti.performance" on path: dexpathlist[[],nativelibrarydirectories=[/system/lib, /vendor/lib]] 08-15 12:24:07.597 2581-2581/com.lteii.hello v/boostframework: boostframework() : mperf = null 08-15 12:24:07.598 2581-2581/com.lteii.hello i/system.out: killing thread 08-15 12:24:07.598 2581-2671/com.lteii.hello i/system.out: thread killed 08-15 12:24:07.622 2581-2581/com.lteii.hello i/system.out: killing thread 08-15 12:24:07.724 2581-2581/com.lteii.hello i/system.out: killing thread 08-15 12:24:07.746 2581-2581/com.lteii.hello i/system.out: killing thread 08-15 12:24:07.758 2581-2581/com.lteii.hello i/system.out: killing thread 08-15 12:24:07.760 2581-2581/com.lteii.hello d/viewrootimpl@e1e91f8[mainactivity]: viewpostimeinputstage processpointer 1 08-15 12:24:07.760 2581-2581/com.lteii.hello i/system.out: killing thread 08-15 12:24:07.980 2581-2667/com.lteii.hello i/system.out: hello thread 08-15 12:24:08.480 2581-2667/com.lteii.hello i/system.out: hello thread 

i don't understand how possible, apparently onresume() method restarts thread everytime kill ontouchevent() method, since when delete onresume(), don't see anymore "hello thread" after "thread killed" anyway should see "thread created" between "thread killed" , "hello thread" need help!

try following:

public class mainactivity extends appcompatactivity {      private mainloop mainloop = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         if (mainloop == null) {             mainloop = new mainloop();             mainloop.start();         } else {             system.out.println("oncreate: thread has not been killed!");         }      }      @override     protected void onresume() {         super.onresume();         if (mainloop == null) {             mainloop = new mainloop();             mainloop.start();         } else {             system.out.println("onresume: thread has not been killed!");         }      }      @override     public boolean ontouchevent(motionevent event) {         if (mainloop != null) {             mainloop.kill();             mainloop = null;         } else {             system.out.println("ontouchevent: thread has not been created!");         }         return false;     } 

that should explain issue. in general practice double-check own code. in best case double-checks never called, in worst case know why things going wrong.

as side-note: thread created runs in infinite loop, drain battery fast doing nothing. @ minimum use thread.sleep or better use scheduledexecutor, or timer if ever have 1 thread.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -