java - What is causing this runnable thread to be skipped -
i have attempted follow examples have found on implementing runnable thread, reason code skipped.
for sake of having better understanding, can tell me preventing thread code executing?
mainactivity.java:
import android.os.bundle; import android.support.v4.app.fragment; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { string textinfo = ""; getmenuinflater().inflate(r.menu.main, menu); textview tv = (textview) findviewbyid(r.id.infotext); textinfo += "testing android runnable thread.\n"; tv.settext(textinfo); runnable runnable = new runnable() { public void run() { global.classtext += "running in thread!\n"; long endtime = system.currenttimemillis() + 20 * 1000; while (system.currenttimemillis() < endtime) { synchronized (this) { try { wait(endtime - system.currenttimemillis()); } catch (exception e) { } } } } }; thread mythread = new thread(runnable); mythread.start(); textinfo += global.classtext; textinfo += "finished.\n"; tv.settext(textinfo); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } }
fragment_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:id="@+id/infotext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/infotext" /> </linearlayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">andorid thread test</string> <string name="action_settings">settings</string> <string name="infotext">android thread test</string> </resources>
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.apollo.androidthreadtest" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="19" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.apollo.androidmysqltest.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
the code doesn't produce errors. runs on android output:
testing androd runnable thread. finished.
the lines in thread ("running in thread!") never displays , debug shows whole block skipped.
since running different thread, cannot expect code execute sequentially like:
thread mythread = new thread(runnable); mythread.start(); textinfo += global.classtext; textinfo += "finished.\n"; tv.settext(textinfo); return true;
by time main thread hits return true, global.classtext
may have not been changed parallel thread yet
to see mean, try changing code this:
thread mythread = new thread(runnable); mythread.start(); while(mythread.isalive()){ //wait till thread finishes } textinfo += global.classtext; textinfo += "finished.\n"; tv.settext(textinfo); return true;
Comments
Post a Comment