停止当前线程问题 [英] Stop Current Thread Issue

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

问题描述

我的问题是我运行一个方法为线程和30秒后我将显示alertdialog并点击alertdialog的OK按钮,我会停止当前线程,但问题是线程是停不下来,下面是我的code和抱歉糟糕的英语交际

 公共类CountDownTimerActivity扩展活动实现Runnable {    线程t;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        T =新的Thread(本);
        t.start();    }    @覆盖
    公共无效的run(){
        // TODO自动生成方法存根
        myTimer.start();
        mDeclaration();
        myTimer.cancel();
    }    私人CountDownTimer myTimer =新CountDownTimer(30000,1000){
        //这会给你30秒计时器滴答各以1秒        公共无效onTick(长millisUntilFinished){        }        公共无效onFinish(){
            t.interrupt();
            //t.stop();
            AlertDialog.Builder警报=新AlertDialog.Builder(
                    CountDownTimerActivity.this);
            alert.setMessage(正在加载...);
            alert.setPositiveButton(OK,
                    新DialogInterface.OnClickListener(){                        @覆盖
                        公共无效的onClick(DialogInterface对话,诠释它){
                            // TODO自动生成方法存根                        }
                    });
            alert.show();
        }
    };    公共无效mDeclaration(){
        的for(int i = 0; I< 100000;我++){
            的System.out.println(打印OK+ I);
        }
    }
}


解决方案

试试这个: -

 公共类CountDownTimerActivity扩展活动实现Runnable {线程t;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    T =新的Thread(本);
    t.start();}@覆盖
公共无效的run(){
    // TODO自动生成方法存根
    myTimer.start();
    mDeclaration();
    myTimer.cancel();
}私人CountDownTimer myTimer =新CountDownTimer(30000,1000){
    //这会给你30秒计时器滴答各以1秒    公共无效onTick(长millisUntilFinished){    }    公共无效onFinish(){        AlertDialog.Builder警报=新AlertDialog.Builder(
                CountDownTimerActivity.this);
        alert.setMessage(正在加载...);
        alert.setPositiveButton(OK,
                新DialogInterface.OnClickListener(){                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                    // t.interrupt();
        t.stop();
                    }
                });
        alert.show();
    }
};公共无效mDeclaration(){
    的for(int i = 0; I< 100000;我++){
        的System.out.println(打印OK+ I);
    }
}

}

My Problem is I am Running one Method into Thread and after 30 Seconds I will display alertdialog and click on alertdialog's ok button i will stop current thread but problem is thread is not stop, following is my Code and sorry for bad english comunication

public class CountDownTimerActivity extends Activity implements Runnable {

    Thread t;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        t = new Thread(this);
        t.start();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        myTimer.start();
        mDeclaration();
        myTimer.cancel();
    }

    private CountDownTimer myTimer = new CountDownTimer(30000, 1000) {
        // This will give you 30 sec timer with each tick at 1 second

        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            t.interrupt();
            //t.stop();
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    CountDownTimerActivity.this);
            alert.setMessage("Loading...");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    });
            alert.show();
        }
    };

    public void mDeclaration() {
        for (int i = 0; i < 100000; i++) {
            System.out.println("Printing OK" + i);
        }
    }
}

解决方案

try this:-

public class CountDownTimerActivity extends Activity implements Runnable {

Thread t;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t = new Thread(this);
    t.start();

}

@Override
public void run() {
    // TODO Auto-generated method stub
    myTimer.start();
    mDeclaration();
    myTimer.cancel();
}

private CountDownTimer myTimer = new CountDownTimer(30000, 1000) {
    // This will give you 30 sec timer with each tick at 1 second

    public void onTick(long millisUntilFinished) {

    }

    public void onFinish() {

        AlertDialog.Builder alert = new AlertDialog.Builder(
                CountDownTimerActivity.this);
        alert.setMessage("Loading...");
        alert.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    //  t.interrupt();
        t.stop();
                    }
                });
        alert.show();
    }
};

public void mDeclaration() {
    for (int i = 0; i < 100000; i++) {
        System.out.println("Printing OK" + i);
    }
}

}

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

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