一个帖子:
public class DrawThread extends Thread {
private RMapCanvas canvas;
final Handler myHandler = new Handler();
final ArrayList<RGroupMapLayer> layers;
public DrawThread(RMapCanvas aCanvas, ArrayList<RGroupMapLayer> arrayOfLayers)
{
canvas = aCanvas;
layers = arrayOfLayers;
}
public void run() {
activity.RaiseNotification(activity.getApplicationContext().getResources().getString(R.string.loading).toString());
for (RGroupMapLayer l : layers)
{
l.validateAndRepairView(false, canvas.renderer, canvas.mapView);
l.render(canvas.renderer, canvas.mapView, canvas.mapEvent, 5000000, 5000000);
if (settings.getPositionFeature() != null)
{
RPointFeatureStyle positionStyle = new RPointFeatureStyle(RPointFeatureStyle.SIMPLE_POINT_STYLE_GPS);
positionStyle.render(settings.getPositionFeature(), renderer, mapView);
}
myHandler.post(new Runnable() {
@Override
public void run() {
canvas.RenderComplete();
}});
}
activity.statusMessage.cancel();
}
public synchronized void requestStop() {
activity.statusMessage.cancel();
this.interrupt();
}
}
调用中断线程的requestStop是不够的。线程一直在运行。有人可以更新代码以告诉我停止此线程的正确方法吗?顺便说一句...... RenderComplete()方法更新了UI(在我的情况下,这是一个问题,因为“旧的”线程不断更新UI,即使新的线程是启动器而旧的线程中断了。)
提前完成。
答案 0 :(得分:2)
通常不建议您自行中断或停止线程。你的线程应该在run方法中有一个标志。当标志为true
时 - 线程正在运行,当标志为false
时 - 线程也在运行,但现在它没有做任何事情。系统将在需要时停止此线程。以下是它的外观:
public void run() {
while (flag) {
//do your work
}
}
要“停止”线程,请使用flag = false;
希望这有帮助。