java - Android Threads, Handlers, handleMessage: Updating UI thread from runnable threads within external class files -


firstly quite new android please bare me :)

background: trying convert java (non gui) program run on android. simplicity lets program has following files:

dsgen.java, sinfer.java, main.java

in main() method, objects each of classes dsgen , sinfer initialized , passed new threads , started. each of classes dsgen , sinfer implement runnable public void run() method , both run methods output text system.out.println().

thread 1. generates 100 values of random data thread 2. makes calculations on 100 values of generated data (outputs 100 results)

in android version of program know ui thread handles ui updates. in mainactivity.java file trying declare handler receive messages "thread 2 output data" can used update textview in mainactivity ui thread. app starts executing when press start button on android ui.

problem: when run app textview not update until processing has ended. noticed not output updated textview , 40/100 calculations make textview.

the actual code program quite long , thought best include pieces relate threads, handler , run method sinfer class.

i hoping can give me pseudo code example or solution dealing multiple threads runnable methods declared within 2 different class files (not inner classes within mainactivity) , ui can updated passing message 1 of threads ui thread.

thank in advance!

public class mainactivity extends activity {      /* ui elements */     private textview output;      /* thread handler */     private handler uihandler;      /* handle message override */     public class uihandler extends handler {          @override         public void handlemessage(message msg) {             output.append(msg.obj.tostring());             super.handlemessage(msg);         }     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_run_fsi);          output = (textview) findviewbyid(r.id.displaytextview);         uihandler = new uihandler();          setstartbuttonclicklistener();      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.activity_run_fsi, menu);         return true;     }       private void setstartbuttonclicklistener() {         button startbutton = (button) findviewbyid(r.id.startbutton);         startbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                  runfsi();              }         });     }      private void runfsi() {         // initialize sinfer , pass uihandler          sinfer inferer = sinfer.getinstance(/*other variables*/, uihandler);           // initialize dsgen         dsgenerator dsgen;          thread t1 = new thread(dsgen);         thread t2 = new thread(inferer);         t1.start();         t1.yield();         t2.start();         t2.yield();          try {             thread.sleep(10000);         } catch (exception e) {         }          inferer.stop();         dsgen.stop();      }  } 

the run method defined inside sinfer follows (the system.out code left on original java code)

@override public void run() {      // system.out.println("inference: thread started...");      while (stopflag == false) {         try {             // system.out.println("sinference: run.........");             thread.sleep(200);             inferenceresult = doinference();              /* send result ui thread */              message msg = message.obtain(uihandler);             msg.obj = inferenceresult.tostring();             uihandler.sendmessage(msg);          } catch (interruptedexception e) {             // system.out.println(e.getmessage());         }     } } 

you running thread.sleep(10000); in main thread (onclick called in ui thread), nothing updated in ui during 10 seconds. try calling runfsi method in separate thread.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -