如何在Java API文档中了解此接口Executor示例

问题描述

任何人都可以帮助详细解释这段代码吗?

 class SerialExecutor implements Executor {
   final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
   final Executor executor;
   Runnable active;

   SerialExecutor(Executor executor) {
     this.executor = executor;
   }

   public synchronized void execute(final Runnable r) {
     tasks.offer(new Runnable() {
       public void run() {
         try {
           r.run();
         } finally {
           scheduleNext();
         }
       }
     });
     if (active == null) {
       scheduleNext();
     }
   }

   protected synchronized void scheduleNext() {
     if ((active = tasks.poll()) != null) {
       executor.execute(active);
     }
   }
 }

我正在学习Java并发编程。当我查看这段代码时,我感到迷茫。主要有两点使我感到困惑:

  1. 为什么要在执行器中定义Executor executor,这是如何工作的?
  2. public synchronized void execute(final Runnable r)中创建new Runnable(){},并在这些Runnable中调用Runnable r.run()?这是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)