我试图按照我在实现可运行线程时找到的示例,但由于某种原因,代码被跳过。
为了更好地理解,有人能告诉我阻止线程代码执行的原因吗?
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. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a 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>
代码不会产生任何错误。它在android上运行输出:
测试Androd Runnable线程。 结束。
线程中的行(&#34;在线程中运行!&#34;)从不显示和调试显示整个块被跳过。
答案 0 :(得分:2)
由于您运行的是另一个线程,因此您不能指望代码按顺序执行:
Thread mythread = new Thread(runnable);
mythread.start();
textinfo += Global.classtext;
textinfo += "Finished.\n";
tv.setText(textinfo);
return true;
当主线程返回true时,并行线程可能尚未更改Global.classtext
要了解我的意思,请尝试将代码更改为:
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;