View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.util.concurrent;
17  
18  import java.util.Collections;
19  import java.util.Iterator;
20  import java.util.concurrent.Callable;
21  import java.util.concurrent.TimeUnit;
22  
23  /**
24   * The {@link EventExecutor} is a special {@link EventExecutorGroup} which comes
25   * with some handy methods to see if a {@link Thread} is executed in a event loop.
26   * Besides this, it also extends the {@link EventExecutorGroup} to allow for a generic
27   * way to access methods.
28   *
29   */
30  public interface EventExecutor extends EventExecutorGroup, FuturePromiseFactory {
31  
32      /**
33       * Returns a reference to itself.
34       */
35      @Override
36      default EventExecutor next() {
37          return this;
38      }
39  
40      @Override
41      default Iterator<EventExecutor> iterator() {
42          return Collections.singleton(this).iterator();
43      }
44  
45      /**
46       * Calls {@link #inEventLoop(Thread)} with {@link Thread#currentThread()} as argument
47       */
48      default boolean inEventLoop() {
49          return inEventLoop(Thread.currentThread());
50      }
51  
52      /**
53       * Return {@code true} if the given {@link Thread} is executed in the event loop,
54       * {@code false} otherwise.
55       */
56      boolean inEventLoop(Thread thread);
57  
58      /**
59       * Return a new {@link Promise}.
60       */
61      default <V> Promise<V> newPromise() {
62          return new DefaultPromise<>(this);
63      }
64  
65      /**
66       * Create a new {@link Future} which is marked as succeeded already. So {@link Future#isSuccess()}
67       * will return {@code true}. All {@link FutureListener} added to it will be notified directly. Also
68       * every call of blocking methods will just return without blocking.
69       */
70      default <V> Future<V> newSucceededFuture(V result) {
71          return DefaultPromise.newSuccessfulPromise(this, result).asFuture();
72      }
73  
74      /**
75       * Create a new {@link Future} which is marked as failed already. So {@link Future#isSuccess()}
76       * will return {@code false}. All {@link FutureListener} added to it will be notified directly. Also
77       * every call of blocking methods will just return without blocking.
78       */
79      default <V> Future<V> newFailedFuture(Throwable cause) {
80          return DefaultPromise.<V>newFailedPromise(this, cause).asFuture();
81      }
82  
83      // Force the implementing class to implement these methods itself. This is needed as
84      // EventExecutorGroup provides default implementations that call next() which would lead to
85      // and infinite loop if not implemented differently in the EventExecutor itself.
86      @Override
87      Future<Void> submit(Runnable task);
88  
89      @Override
90      <T> Future<T> submit(Runnable task, T result);
91  
92      @Override
93      <T> Future<T> submit(Callable<T> task);
94  
95      @Override
96      Future<Void> schedule(Runnable task, long delay, TimeUnit unit);
97  
98      @Override
99      <V> Future<V> schedule(Callable<V> task, long delay, TimeUnit unit);
100 
101     @Override
102     Future<Void> scheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit);
103 
104     @Override
105     Future<Void> scheduleWithFixedDelay(Runnable task, long initialDelay, long delay, TimeUnit unit);
106 
107     @Override
108     void execute(Runnable task);
109 }