android线程:停止计算量大的线程

发布于 2024-11-18 04:26:20 字数 1085 浏览 1 评论 0原文

我正在我的 Android 应用程序中进行一些繁重的计算。由于我不想阻塞 UI 线程,因此我在一个扩展了 Thread 的单独 WorkerThread 中进行计算。这很好用。但我无法停止线程。停止线程的正常方法是使用interrupt()或一个易失性变量来告诉线程它应该结束。但这仅适用于具有某种循环的线程。我正在使用外部库在线程中进行计算,并且无法将 isInterrupted() 检查放入该库中。停止线程的糟糕方法是使用 stop()-Methode,该方法已被弃用,但在我的情况下是正确的方法,因为线程仅操作内部值。不幸的是,Android 不支持 stop(),因为它不支持已弃用的线程方法。

10-18 10:26:39.382: ERROR/global(13919): Deprecated Thread methods are not supported.
10-18 10:26:39.382: ERROR/global(13919): java.lang.UnsupportedOperationException

你现在还有其他方法来停止android中的线程吗? 提前致谢! Tobias

PS:一些代码来显示问题:

public void method()
{
    WorkerThread t = new WorkerThread();
    t.start();
    synchronized (this)
    {
        try
        {
            wait(1000);
        } catch (InterruptedException e)
        {
            Log.e("",e.toString(),e);
        }    
    }

    //t.stop(); //not supported in android
    //t.interrupt(); //can not be checked by the thread    
}

class WorkerThread extends Thread
{
    public void run()
    {
        externalLibrary.heavyComputation();
    }
}

I am doing some heavy computation in my android application. Since i dont want to block the UI-thread, I do the computation in a seperate WorkerThread, which extends Thread. This works fine. But i have trouble stopping the thread. The normal way to stop a thread is either using interrupt() oder a volatile variable to tell the thread, that it should end. But this works only in Threads with some kind of loop. I am doing computation in the thread by using external libraries and I cannot put the isInterrupted()-check into this library. The bad way to stop a thread is using the stop()-Methode, which is deprecated, but in my case the right way, since the thread manipulates only internal values. Unfortunatly Android doesnt support stop(), since it doesn't support deprecated thread methods.

10-18 10:26:39.382: ERROR/global(13919): Deprecated Thread methods are not supported.
10-18 10:26:39.382: ERROR/global(13919): java.lang.UnsupportedOperationException

Do you now any other method to stop a thread in android?
Thanks in advance!
Tobias

PS: some code to show the problem:

public void method()
{
    WorkerThread t = new WorkerThread();
    t.start();
    synchronized (this)
    {
        try
        {
            wait(1000);
        } catch (InterruptedException e)
        {
            Log.e("",e.toString(),e);
        }    
    }

    //t.stop(); //not supported in android
    //t.interrupt(); //can not be checked by the thread    
}

class WorkerThread extends Thread
{
    public void run()
    {
        externalLibrary.heavyComputation();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁对谁错谁最难过 2024-11-25 04:26:20

一般来说,除非您的外部库提供某种协议来停止计算,否则无法优雅地停止计算。许多写得好的库都会提供这样的协议,所以看看它的 api。它还可能尊重 interrupt() 调用。可能您已经检查过它,但退出可能还需要一些时间。

Generally, unless your external library provides some protocol to stop computation there is no way to stop it gracefully. Many well written libraries will do provide such protocol, so look at its api. Also it may respect interrupt() call. May be you already checked it but it may also take some time before it exits.

抱猫软卧 2024-11-25 04:26:20

看看AsyncTask。这是执行后台任务的更合适、更舒适的方式。
正如您可以在文档中阅读的那样:

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Look at AsyncTask. It is more proper and far more comfortable way to implement background tasks.
As you can read in documentation:

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文