线程阻止我的Android UI

我的Android应用程序中的java Threads有问题。 我的嵌套线程阻止了我的UI,我该如何解决这个问题呢?

MyClass.java

package com.knobik.gadu; import android.util.Log; public class MyClass { public void StartTheThread() { Thread Nested = new Thread( new NestedThread() ); Nested.run(); } private class NestedThread implements Runnable { public void run() { while (true) { Log.d( "DUPA!", "debug log SPAM!!" ); } } } } 

这就是我运行它的方式:

 package com.knobik.gadu; import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; public class AndroidGadu extends Activity { public static final String LogAct = "AndroidGadu"; public void OnClickTest(View v) { MyClass test = new MyClass(); test.StartTheThread(); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

你可以帮帮我吗? 我文明被卡住了;)

我的嵌套线程阻止了我的UI

您需要使用.start()而不是.run() 。 也就是说,替换

 Nested.run(); 

 Nested.start(); 

run只是一个普通的方法start()是实际生成一个新线程的方法,该线程反过来运行run

这段代码是个问题:

 while (true) { Log.d( "DUPA!", "debug log SPAM!!" ); } 

忙碌的等待会占用CPU。

 package com.knobik.gadu; import android.util.Log; public class MyClass { public void StartTheThread() { Thread Nested = new Thread( new NestedThread() ); Nested.run(); // Change this to Nested.start(); } private class NestedThread implements Runnable { public void run() { while (true) { // infinite loop logical error Log.d( "DUPA!", "debug log SPAM!!" ); } } } }