如何延迟衍生的线程时UI线程的Runnable运行是不是一种选择? [英] How to delay spawned thread when a Runnable run on UI thread isn't an option?
问题描述
我曾与一个previous的问题,我不能肯定我的code是否有内存泄漏问题。它被UI线程上运行,因此阻断几个答案担心。这是真实的,它运行在UI线程并不会产生一个新的..
I had issues with a previous question where I was unsure whether my code had a memory leak. A few answers were concerned with it being run on the UI thread and so blocking. It is true, it runs on the UI thread and doesn't spawn a new one..
所以要解决它,我用发
而不是处理程序
产卵UI之外一个新的线程。现在的问题是,我不能管理我一样在用户界面线程中有一个人跑去做耽误了。
So to solve it I use Thread
instead of Handler
to spawn a new thread outside of UI. The problem now is that I can't manage to delay it like I did with the one ran in the UI thread.
这是我的previous问题在我的原来的UI线程code是:的是内存泄漏这个Runnable的安全和以下是更新code这产生一个新的线程:
This is my previous question where my original UI thread code is: Is this Runnable safe from memory leak? and the following is the updated code which spawns a new thread:
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.util.Log;
import java.lang.ref.WeakReference;
public class HelloWorldActivity extends Activity
{
private static TextView txtview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtview = (TextView) findViewById(R.id.mainview);
Thread t = new Thread(new WeakRunnable(txtview));
t.start();
}
private static final class WeakRunnable implements Runnable {
private final WeakReference<TextView> mtextview;
protected WeakRunnable(TextView textview){
mtextview = new WeakReference<TextView>(textview);
}
@Override
public void run() {
TextView textview = mtextview.get();
if (textview != null) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
int test = 5*5;
txtview.setText("Hola Mundo"+test);
}
Log.d("com.example.helloworld", "" + Thread.currentThread().getName()); // Outputs "Thread-<num>" if not running on UI thread
}
}
}
它只是设置视图文本和追加的结果 5 * 5
。
当我启动它退出本身应用程序,我不知道为什么。有个声音告诉我,我推迟了错误的方式或者使用 runOnUiThread
错了。即使在更改 txtview.setText(世界报霍拉+测试);
到 runOnUiThread(txtview.setText(世界报霍拉+测试));
不会编译给错误:无效类型这里不允许
。
As soon as I start the app it quits itself and I don't get why. Something tells me I'm delaying it the wrong way or using runOnUiThread
wrong. Even changing txtview.setText("Hola Mundo"+test);
to runOnUiThread( txtview.setText("Hola Mundo"+test) );
doesn't compile giving error: 'void' type not allowed here
.
在一言以蔽之:计算( 5 * 5
在这种情况下)应该在一个单独的线程来完成,以避免阻塞主要(UI)线程和文本应在服用computated项目从单独的线程的用户界面进行设置。你自己的一个简单的例子就可以了了。
In a nutshell: Computation (5*5
in this case) should be done on a separate thread to avoid blocking the main (UI) thread, and the text should be set on the UI taking the computated item from the separate thread. A simple example of your own would be fine too.
更新
我已经发布的答案,我自己的问题实施的AsyncTask
。
推荐答案
您可以使用状态,直到它被满足,而不是使用Thread.sleep代码();
You can use condition until it is met instead of using thread.sleep();
Condition.wait(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return textview != null;
}
});
这篇关于如何延迟衍生的线程时UI线程的Runnable运行是不是一种选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!