【问题标题】:Implementation of Half Sync Half Asyncc pattern of POSA in Android AsynctaskAndroid Asynctask中POSA的Half Sync Half Asyncc模式的实现
【发布时间】:2013-11-14 11:14:27
【问题描述】:

请参考下面这段取自 Android 的 Asynctask 的代码。我认为这是面向模式的软件架构书中并发模式的半同步半异步模式的一个例子。有人可以确认一下吗?

private static class SerialExecutor implements Executor {    
    final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();        
    Runnable mActive;        

    public synchronized void execute(final Runnable r) {        
        mTasks.offer(new Runnable() {        
            public void run() {        
                try {        
                    r.run();        
                } finally {        
                    scheduleNext();
                }        
            }
        });

        if (mActive == null) {        
            scheduleNext();
        }        
    }        

    protected synchronized void scheduleNext() {        
        if ((mActive = mTasks.poll()) != null) {        
            THREAD_POOL_EXECUTOR.execute(mActive);        
        }
    }    
}

【问题讨论】:

  • 我已经明白了。当然 SerialExecutor 是 Half Sync - Half Async 模式的一个例子。这是我为 Asynctask 内部编写的文章...docs.google.com/document/d/…

标签: android design-patterns android-asynctask


【解决方案1】:

所以是的,我可以确认这是真的。

此外,我亲自与 POSA2 的作者之一 Doug Schmidt(我的博士顾问)讨论了这个问题,只是为了 100% 确定(并让您更有信心)。他给了我一个视频链接,他在视频中深入讨论了这个话题。

这是他在范德比尔特大学教授的一门课程中的内容,他在课程中介绍了高级操作原理(例如并发性),其中使用 Android 作为示例操作系统/平台。

http://www.youtube.com/watch?v=XZ29DAcmcLE

此外,该主题已在过去介绍过,并将在 Coursera 上重复出现的 POSA 课程中再次介绍。这也讨论了软件模式并在 Android 框架中展示了它们。

https://www.coursera.org/course/posa

【讨论】:

  • 感谢您的确认。我喜欢 Doug 在 Coursera 的讲座。这些内容非常丰富。我还想借此机会感谢 Doug 和 Coursera 启发了全球许多像我这样热心的学习者...
猜你喜欢
  • 2023-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
相关资源
最近更新 更多