【问题标题】:Not able to resume my thread in android无法在android中恢复我的线程
【发布时间】:2015-10-01 17:03:08
【问题描述】:

我知道这里有很多关于此主题的问题和答案,但我无法解决以下问题以在我的应用中恢复线程。

这是我的可运行文件:-

Runnable ViewPagerVisibleScroll= new Runnable() {
    @Override
    public void run() {
                if (i <= mAdapter.getCount() - 1) {
                    verticalViewPager.setCurrentItem(i, true);
                    handler.postDelayed(ViewPagerVisibleScroll, 3000);

                    i++;
                    while (isPaused) {
                        synchronized (ViewPagerVisibleScroll) {
                            // wait for resume() to be called
                            try {
                                ViewPagerVisibleScroll.wait();
                                //  isPaused = false;
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }
                }
    }
};

暂停和恢复方法:-

public void pause() {
    synchronized (ViewPagerVisibleScroll) {
        isPaused = true;
    }
}

public synchronized void resume() {
    synchronized (ViewPagerVisibleScroll) {
        isPaused = false;
        // notify anybody waiting on "this"
        ViewPagerVisibleScroll.notify();
    }
}

我的问题是当我调用 pause 方法时线程将 pause() 但当我调用 resume() 时它不会恢复。请帮我解决这个问题。

【问题讨论】:

    标签: java android multithreading runnable


    【解决方案1】:

    首先,我看到了这段代码:

    public void pause() {
        synchronized (ViewPagerVisibleScroll) {
            isPaused = true;   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
    }
    
    public synchronized void resume() {
        synchronized (ViewPagerVisibleScroll) {
            isPaused = false;
            // notify anybody waiting on "this"
            ViewPagerVisibleScroll.notify();
        }
    }
    

    还有这个:

    while (isPaused)
    

    我可以保证,如果您调用方法pause(),即您将isPaused 设置为true,那么当isPaused 为真时,此循环将被执行。你应该使用!isPaused:

    while (!isPaused)
    

    暂停做某事。这只是一个介绍。

    所以,回到你的问题,我建议你下一步:
    1)。创建static final对象进行同步:

    private static final Object syncObj = new Object();
    private static final Object stopSyncObj = new Object();
    

    2)。将您的可运行文件实现为内部类:

    public class MyRunnable implements Runnable {
    
            private boolean isPaused;
            private boolean isStopped;
    
            public MyRunnable(){
                synchronized (stopSyncObj){
                    isStopped = false;
                }
                synchronized (syncObj){
                    isPaused = false;
                }
            }
    
            @Override
            public void run() {
                if (i <= mAdapter.getCount() - 1) {
                    verticalViewPager.setCurrentItem(i, true);
                    handler.postDelayed(ViewPagerVisibleScroll, 3000);
    
                    i++;
    
                    //Check thread
                    boolean isStopped = false;
    
                    while (!isStopped) {
                        synchronized (syncObj) {
                            boolean isPaused = false;
                            synchronized (syncObj){
                                isPaused = this.isPaused;
                            }
    
                            if(isPaused) {
                                // wait for resume() to be called
                                try {
                                    syncObj.wait();
                                    //  isPaused = false;
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }else{
                                //do somethinghere
    
                            }
                        }
    
                        //Check 'stop' flag
                        synchronized (stopSyncObj){
                            isStopped = this.isStopped;
                        }
                    }
                }
            }
    
            //This method stops runnable forever
            public void stopRunnable(){
                synchronized (stopSyncObj){
                    isStopped = true;
                }
            }
    
            public void pause(){
                synchronized (syncObj){
                    isPaused = true;
                }
            }
    
            public void resume(){
                synchronized (syncObj){
                    isPaused = false;
                    syncObj.notifyAll();
                }
            }
        }
    

    3)。使用它:

    Runnable ViewPagerVisibleScroll = new MyRunnable();
    

    更新答案
    对于自动滚动使用ScheduledThreadPoolExecutor:

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
            executor.schedule(new Runnable() {
                @Override
                public void run() {
                    //scroll viewpager
                }
            }, 3, TimeUnit.SECONDS);
    

    要阻止它使用:

    executor.shutdown();
    

    【讨论】:

    • 但这是一个很奇怪的代码,你想用的时候我也画不出来。你想做什么?
    • 我有自动滑动垂直浏览器,我想用按钮暂停和恢复它。
    • @HimanshuRathore,你为什么不先说?请查看更新的答案
    • @HimanshuRathore,你也可以尝试使用这个库:github.com/Trinea/android-auto-scroll-view-pager
    • @HimanshuRathore,请发布更多信息。哪个变体不起作用?你怎么检查的?发布您的代码,当然如果您需要帮助...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多