简体   繁体   English

启动和停止工作线程?

[英]start and stop worker thread?

Am creating an app in which background service decrypt downloaded file in the folder and will show file the file. 我正在创建一个应用程序,其中后台服务在该文件夹中解密下载的文件,并将显示该文件。 Once user click on selected file it will start decrypting, if user click on that button decryption will stop and will start with new file. 用户单击所选文件后,它将开始解密,如果用户单击该按钮,解密将停止并以新文件开始。 I tried using thread for to start and stop this task. 我尝试使用线程来启动和停止此任务。

if (mUpdate!=null) {
             mUpdate.interrupt();
         }

         mUpdate=new Thread() {
             @Override
             public void run() {
                 while (!interrupted()) {
                     // decryption of file and showing file to user
                 }
             }
         };
         mUpdate.start();

I would write something like that if you want checking if thread is stopped in while loop: 如果您想检查线程是否在while循环中停止,我会写类似的内容:

class StoppableThread extends Thread {
    private AtomicBoolean mIsStopped = new AtomicBoolean(false);
    public void stopThread() {
        mIsStopped.set(true);
    }
    public boolean isThreadStopped() {
        return mIsStopped.get();
    }
}

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

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