如何在Android / Java中暂停当前循环而不暂停当前正在运行的线程?

huangapple go评论65阅读模式
英文:

How to pause the current loop without pausing current running thread in Android / Java?

问题

在我的安卓应用程序中,在我的片段中,我有一个while循环,类似于

startMs=0, endMs=30000;
while(i < 10){
    new Mp4Composer(inputPath, destPath)
                                .trim(startMs, endMs)
                                .listener(new Mp4Composer.Listener() {
                                    @Override
                                    public void onCompleted(){

                                    }
                                })
                                .start();
    startMs = endMs;
    endMs += 30000;
    i++;
}

这里的new Mp4Composer是一个线程...这个任务在每次循环迭代时执行...而不会完成上一个任务(任务仍处于处理状态)...循环跳到下一个迭代。所以现有任务不会产生任何输出...并且由于循环的原因跳到了下一个任务。

因此,我希望while循环在完成每个任务的new Mp4Composer之后等待。通过使用**public void onCompleted()**方法,我们能够确定何时完成每个任务的异步任务。

并且我不应该暂停当前运行的线程(在其中运行while循环的类线程)。原因是当我暂停放置循环的类线程时,整个UI和我的安卓应用程序都会被暂停。我对线程没有太多的了解。

英文:

In my android application, In my fragment, I have one while loop like

startMs=0, endMs=30000;
while(i&lt;10){
    new Mp4Composer(inputPath, destPath)
                                .trim(startMs, endMs)
                                .listener(new Mp4Composer.Listener() {
                                   @Overide
                                     public void onCompleted(){

                                        }
                                   }
                                .start();
startMs= endMs;
endMs+=30000;
i++
}

Here new Mp4Composer is a Thread... This task is executed for each iteration of the loop..without completing the previous task (on task still in processing state)... the loop jumped to the next iteration.so the existing task doesn't produce any output... and jumped to next task because of the loop.

So here what I want is while loop should wait to complete new Mp4Composer each task. By using public void onCompleted() method... we able to identify when that async task will finish for each task.

And here I should not pause the current running thread (where the class while loop running). The reason is when I pause while loop placed class thread, the total UI, and my android application gets paused. I haven't much knowledge about Thread.

答案1

得分: 1

使用处理程序而不是while循环。

英文:

use handler instead of while loop

答案2

得分: 1

Handler mHandler = new Handler();
int startMs = 0, endMs = 30000, i = 0;
Runnable action = new Runnable() {
    @Override
    public void run() {
        new Mp4Composer(inputPath, destPath)
            .trim(startMs, endMs)
            .listener(new Mp4Composer.Listener() {
                @Override
                public void onCompleted() {
                    goNext();
                }
            })
            .start();
    }
};

void goNext() {
    if (i < 10) {
        startMs = endMs;
        endMs += 30000;
        i++;
        mHandler.postDelayed(action, 2000); //2 second
    }
}
英文:
Handler mHandler = new Handler();
int startMs=0, endMs=30000, i=0;
Runnable action = new Runnable(){
    @Override
    public void run() {
        new Mp4Composer(inputPath, destPath)
                .trim(startMs, endMs)
                .listener(new Mp4Composer.Listener() {
                    @Overide
                    public void onCompleted(){
                        goNext();
                    }
                }
                .start();
    }
};
void goNext(){
    if(i &lt; 10) {
        startMs = endMs;
        endMs += 30000;
        i++;
        mHandler.postDelayed(action, 2000); //2 second
    }
}

huangapple
  • 本文由 发表于 2020年10月17日 00:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定