线程阻塞了我的 Android UI
我的 Android 应用程序中的 java 线程有问题。我的嵌套线程阻塞了我的 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);
}
}
你能帮助我吗?我真的被困住了;)
I have a problem with java Threads in my Android app. My nested thread blocks my UI, how can i resolve this?
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!!" );
}
}
}
}
and this is how i run it:
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);
}
}
Could you help me? I'm literaly stuck ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
.start()
而不是.run()
。也就是说,替换为
(
run
只是一个普通方法。start()
是实际生成一个新线程的方法,该线程又运行run
代码>)You need to use
.start()
instead of.run()
. That is, replacewith
(
run
is just an ordinary method.start()
is the method that actually spawns a new thread, which in turn runsrun
)这段代码有一个问题:
繁忙的等待占用了CPU。
This code is a problem:
A busy wait hogs the CPU.