简体   繁体   English

线程可运行-停止并继续

[英]Thread Runnable - stop and resume

Hello i'm new in Android(Java), and i have a problem with the use of thread 您好,我是Android(Java)的新手,我在使用线程时遇到问题

I define e new Thread timed (every 5 seconds) inside a class of my android Project. 我在我的android Project的类中定义了一个新的线程(每5秒定时)。 The "mContinueThread" variable is used to cicle every 5 seconds “ mContinueThread”变量用于每5秒更新一次

r = new Runnable() {
    public void run() {
        while (mContinueThread) {
            try {
                Thread.sleep(MILLISEC_BEFORE_RELOAD);
                mHandler.sendEmptyMessage(GET_TRACKING);
            }
            catch (Exception e)
            {

            }
        }
    }
};
t = new Thread(r);

In the CLass there is a method StartTrack() that starts with Thread 在CLass中,有一个以Thread开头的StartTrack()方法

public void StartTrack()
{
    mContinueThread=true;
    if (!mThreadIsStarted)
    {
        mThreadIsStarted=true;
        t.start();
    }
    else
    {

    }
}

and there is also a method Logout that stop the thread, using the "mContinueThread" variable: 还有一个方法Logout使用“ mContinueThread”变量来停止线程:

public void LogOut()
{
    //STOP THREAD
    mContinueThread=false;
    ....
}

If in the class Logout() method is executed the thread is stopped, but if the StartTrack() method is called again I don't know how to restart the execution of the thread. 如果在类Logout()方法中执行了该线程,则停止该线程,但是如果再次调用StartTrack()方法,我将不知道如何重新开始执行该线程。

Can you Help Me? 你能帮助我吗?

You can use AsyncTask in Android. 您可以在Android中使用AsyncTask。 This will get rid of the burden of managing the threads manually. 这将摆脱手动管理线程的负担。 Please visit http://developer.android.com/reference/android/os/AsyncTask.html 请访问http://developer.android.com/reference/android/os/AsyncTask.html

You cannot re-start a thread. 您无法重新启动线程。 Once thread is finished execution it will reach the DEAD state. 一旦线程完成执行,它将达到DEAD状态。 And whatever is DEAD cannot be brought back to life again, neither in real world nor in JAVA world. 无论是DEAD还是DEAD,无论在现实世界还是JAVA世界中,都无法重现。

You have no way to restart a thread as long as it exited. 只要线程退出,就无法重新启动它。 You can just start a new start. 您可以重新开始。

I solved so: 我这样解决了:

In my class I just define the Runnable object, but not the new Thread. 在我的课程中,我只定义了Runnable对象,而不是新的Thread。

In the StartTrack method(), if the thread has not yet been instantiated, I create and start 在StartTrack method()中,如果尚未实例化线程,则创建并启动

public void StartTrack()
{
    mContinueThread=true;

    if (!mThreadIsStarted)
    {
        mThreadIsStarted=true;
        t = new Thread(r);
        t.start();
    }
}

In the "LogOut()" method, if Thread is started, I Stop It, and I set It to Null. 在“ LogOut()”方法中,如果启动了线程,则将其停止,并将其设置为Null。

In this way, at the next call of "StartTrack()" method, I can recreate it again 这样,在下一次调用“ StartTrack()”方法时,我可以再次重新创建它

public void LogOut()
{
    mContinueThread=false;
    if (mThreadIsStarted)
    {
        //THREAD STOP
        mContinueThread=false;
        mThreadIsStarted=false;

        //THREAD TO NULL
        t=null;
    }

    ...
}

I suggest it's better to use something like Timer instead of thread. 我建议最好使用Timer之类的东西来代替线程。

http://developer.android.com/reference/java/util/Timer.html http://developer.android.com/reference/java/util/Timer.html

Then you can do cancel() if you want to stop execution of your task and resume it by scheduling new one. 然后,如果要停止执行任务并通过计划新任务来恢复执行,可以执行cancel()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM