简体   繁体   English

如何停止 Android 中的工作线程

[英]How to stop worker thread in Android

I found and use some method bellow but it is not work for me:我在下面找到并使用了一些方法,但它对我不起作用:

myThread.stop() //it is not safe but I am tried that
myThread.interupt
  • Here is my program: I wanna play video using Videoview when video finish.这是我的程序:我想在视频播放完毕后使用 Videoview 播放视频。 If user no choose the next video in 120s then my app will finish.如果用户在 120 秒内没有选择下一个视频,那么我的应用程序将完成。 My video view code:我的视频查看代码:

     Uri uri = Uri.parse(filePath); videoView = findViewById(R.id.videoView); videoView.setVideoURI(uri); waitingThread w8 = new waitingThread(); //set params video before play videoView.setOnPreparedListener(mediaPlayer -> { PlaybackParams playbackParams = new PlaybackParams(); playbackParams.setSpeed(DeviceConfig.getInstance().getVideoPeed());// 1.25 1.5 2 2.5 mediaPlayer.setPlaybackParams(playbackParams); // I am tryied using stop thread here // w8.stop() // or w8.interrupt(); videoView.start(); }); videoView.setOnErrorListener((mediaPlayer, i, i1) -> { Log.d(TAG,"Video Error"); //send error message to server return false; }); //I call thread when video complete videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { **waiting();** -> w8.start //edited i start thread here } });

My thread waiting我的线程等待

private  class waitingThread extends Thread{

    public void run() {
        try {
            while (!isInterrupted()) {

                timeCount ++;
                Thread.sleep(1000);
                Log.d(TAG, "Time count : " + timeCount);
                if(timeCount == 120){
                   finish();
                 }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //i am try to using this funtion but still not work too.
    public void stopping(){
        Thread.currentThread().interrupt();
//            timeCount = 0;
    //            Log.d(TAG, "Stopping, reset time :" + timeCount);
            }

    }

Brief my idea: When video start play, thread waiting will be stopped.简述我的想法:当视频开始播放时,线程等待将停止。 When video finish program will waiting a time if no video chose in time to wait program will finish, if any video chose then thread w8 stop.当视频完成程序将等待一段时间,如果没有及时选择视频等待程序将完成,如果选择了任何视频,则线程 w8 停止。

My problem: when I choose the next video, my thread "w8" still keep running.我的问题:当我选择下一个视频时,我的线程“w8”仍在运行。 That is make my app finished while video playing Plz help me how to fix that problem or any same idea to work are appreciated那就是在播放视频时完成我的应用程序请帮助我如何解决该问题或任何相同的工作想法表示赞赏

You don't want to call interrupt on Thread.currentThread.您不想在 Thread.currentThread 上调用中断。 Thread.currentThread is the thread currently running- it's the thread you're calling the function on. Thread.currentThread 是当前正在运行的线程 - 它是您正在调用 function 的线程。 It's not the thread object you just created.这不是您刚刚创建的线程 object。 Instead it would be this.interrupt().相反,它将是 this.interrupt()。 Or just get rid of the function entirely and call interrupt directly.或者完全摆脱 function 并直接调用中断。

Introducing your own boolean variable might help介绍您自己的 boolean 变量可能会有所帮助

class waitingThread extends Thread{

    boolean stop;

    public void run(){
        while(!stop){
            //your task
        }
     stop = false; // 
    }

    public void stopping(){
        stop= true;
    }
}

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

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