简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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