线故障 [英] thread malfunction

查看:113
本文介绍了线故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个项目中,有2班。 activity_main有2个按钮,按钮1运行一个线程,我想用按钮2停止它,但它不工作,因为当线程运行的Button2无法点击。最后AVD停止程序。请任何建议???

Thnaks提前。

activity_main.xml中

 <按钮
   机器人:ID =@ + ID /按钮1
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   安卓的onClick =gestionbotones
   机器人:文字=线程/>
<按钮
   机器人:ID =@ + ID /按钮2
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   安卓的onClick =gestionbotones
   机器人:文字=线程OFF/>

MainActivity.java

 公共类MainActivity延伸活动{
.......
私人HiloJuego HJ =新HiloJuego();
.......
公共无效gestionbotones(视图v){
    INT ID = v.getId();
    开关(ID){
    案例R.id.button1:
        Log.d(TAG,线程activado);
        hj.setRunning(真);
        hj.setTurno(真);
        hj.run();
        打破;
    案例R.id.button2:// Desactivar
        hj.setRunning(假);
        Log.d(TAG,线程destruído);
       打破;
    默认:
        打破;
    }
}

HiloJuego.java

 包com.example.tocatoca1;
进口android.util.Log;
公共类HiloJuego继承Thread {
    私有静态最后弦乐TAG = HiloJuego.class.getSimpleName();    私人布尔运行;
    私人布尔turno;
    公共无效setRunning(布尔运行){
        this.running =运行;
    }
    公共无效setTurno(布尔turno){
        this.turno = turno;
    }
    公共HiloJuego(){
        超();
    }
    @覆盖
    公共无效的run(){
        Log.d(TAG启动游戏循环);
    而(运行){
        如果(turno){
                Log.d(TAGTurno Ordenador);
        }其他{
            Log.d(TAGTurno Jugador);
        }
    } //最后结束
}
}


在一个单独的线程

解决方案

要运行实例,它是线程#启动(),而不是线程#的run()线程#的run()将不会创建一个新的线程,但只运行的run()在当前线程(这是UI线程,这就是为什么你会得到ANR)。

此外它更好地实现Runnable接口,而不是扩展Thread

I have this project, with 2 classes. activity_main has 2 buttons, button1 runs a thread and I want to stop it with button2, but it does not work, because while the thread is running button2 is not clickable. Finally AVD stops the program. Please, any suggestion???

Thnaks in advance.

activity_main.xml

<Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="gestionbotones"
   android:text="Thread ON" />
<Button
   android:id="@+id/button2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="gestionbotones"
   android:text="Thread OFF" />

MainActivity.java

public class MainActivity extends Activity {
.......
private HiloJuego hj = new HiloJuego();
.......
public void gestionbotones (View v){
    int id = v.getId();
    switch(id){
    case R.id.button1 :
        Log.d(TAG, "Thread activado");
        hj.setRunning(true);
        hj.setTurno(true);
        hj.run();
        break;
    case R.id.button2:        //    Desactivar
        hj.setRunning(false);
        Log.d(TAG, "Thread destruído");
       break;
    default:
        break;
    }
}

HiloJuego.java

package com.example.tocatoca1;
import android.util.Log;
public class HiloJuego extends Thread {  
    private static final String TAG = HiloJuego.class.getSimpleName();

    private boolean running;
    private boolean turno;
    public void setRunning(boolean running) {
        this.running = running;
    }
    public void setTurno(boolean turno){
        this.turno=turno;
    }
    public HiloJuego() {
        super();
    }
    @Override
    public void run() {
        Log.d(TAG, "Starting game loop");
    while (running) {
        if (turno){
                Log.d(TAG, "Turno Ordenador");
        } else{
            Log.d(TAG, "Turno Jugador");
        }
    }   // end finally
}
}

解决方案

To run a Thread instance in a separate thread, it's Thread#start(), not Thread#run(). Thread#run() will not create a new thread but just run run() in the current thread (which is UI Thread, this is why you get ANR).

Also it's better to implement Runnable than to extend Thread.

这篇关于线故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆