线程冻结 UI
我有一个按钮,当单击该按钮时,会调用 thead 来更新用户 ui:
averageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Testing().execute();
}
});
该方法会被执行,但会将 UI 冻结 10 秒。我想每次调用averageMiles.append(mile)方法时更新UI。
class Testing extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void unused) {
int x = 0;
while (true) {
if (x == 10) {
break;
}
ArrayList<Double> milesList = new ArrayList<>();
if (x == 0) {
averageMiles.setText("mph: ");
}
String mile = milesValue.getText().toString();
if (!isNum(mile)) {
continue;
}
averageMiles.append(mile);
milesList.add(Double.valueOf(mile.trim()));
x++;
if (x == 10) {
averageMiles.append("\nAverage: " + getAverage(milesList));
return ;
} else {
averageMiles.append(", ");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I have this button that when clicked calls a thead to update user ui:
averageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Testing().execute();
}
});
The method gets executed but it freezes the UI for 10 seconds. I want to update the UI everytime I call the averageMiles.append(mile) method.
class Testing extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void unused) {
int x = 0;
while (true) {
if (x == 10) {
break;
}
ArrayList<Double> milesList = new ArrayList<>();
if (x == 0) {
averageMiles.setText("mph: ");
}
String mile = milesValue.getText().toString();
if (!isNum(mile)) {
continue;
}
averageMiles.append(mile);
milesList.add(Double.valueOf(mile.trim()));
x++;
if (x == 10) {
averageMiles.append("\nAverage: " + getAverage(milesList));
return ;
} else {
averageMiles.append(", ");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用官方 oracle 和 android 开发人员文档中的 ThreadPool 执行器
Oracle 文档 - java.util.concurrent.ThreadPoolExecutor
Android 文档 - java.util.concurrent.ThreadPoolExecutor
如您所指定您使用的是 java,上面的两个文档是 google 推荐的,如下所示的
AsyncTask
最终目标是 android 说,如果您想更新 UI,只需将
runOnUiThread
与更新 UI 的新可运行对象一起使用,这意味着对于每个 UI 更新,您可能会创建新的短期线程只更新 UI 和线程完成以及垃圾收集示例代码
类的所有成员变量只能在 runOnUiThread 内部访问,例如
count++
count 是MainActivity 的变量
如果你只需要在MyThread
类中放置特定于线程的任何值切勿尝试访问
run()
方法中的任何MainActivity
变量您还可以将
MyThread
编写为单独的类,并在启动线程之前设置类似于th.setRunning
的值。如果您想在线程完成后进行回调,请使用一个接口,该接口将为您提供
MainActivity
中的回调方法,因此它只是核心 java
我创建了一个带有界面的示例
并发执行器接口示例
Use ThreadPool executor from official oracle and android developer documentations
Oracle Docs - java.util.concurrent.ThreadPoolExecutor
Android Docs - java.util.concurrent.ThreadPoolExecutor
as you have specified you are using java the above two documentation is recommended by google as show below for
AsyncTask
The ultimate objective is the android is saying that, if you want to update UI, simply use
runOnUiThread
with a new runnable you update the UI, which means for each UI update you may be creating fresh short term thread which only updates the UI and thread finishes and garbage collectedSample Code
all the member variables of the class should be only accessed inside runOnUiThread, for example
count++
count is a variable of theMainActivity
if you want any value specific to the thread you put only inside theMyThread
classNever try to access any of the
MainActivity
variable inside therun()
methodYou can also write
MyThread
as separate class, and set values similar toth.setRunning
before starting the thread.If you want a callback after the thread is completed use an interface which will give you a callback method in your
MainActivity
So it is simply core java
I have created an example with interface
Concurrent Executor Interface Example