View Javadoc
1   /*
2    * Copyright 2013 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    *   http://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.netty.util.concurrent;
17  
18  import java.util.concurrent.TimeUnit;
19  
20  /**
21   * {@link AbstractEventExecutor} which execute tasks in the callers thread.
22   */
23  public final class ImmediateEventExecutor extends AbstractEventExecutor {
24  
25      public static final ImmediateEventExecutor INSTANCE = new ImmediateEventExecutor();
26  
27      private final Future<?> terminationFuture = new FailedFuture<Object>(
28              GlobalEventExecutor.INSTANCE, new UnsupportedOperationException());
29  
30      private ImmediateEventExecutor() {
31          // Singleton
32      }
33  
34      @Override
35      public boolean inEventLoop() {
36          return true;
37      }
38  
39      @Override
40      public boolean inEventLoop(Thread thread) {
41          return true;
42      }
43  
44      @Override
45      public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
46          return terminationFuture();
47      }
48  
49      @Override
50      public Future<?> terminationFuture() {
51          return terminationFuture;
52      }
53  
54      @Override
55      @Deprecated
56      public void shutdown() { }
57  
58      @Override
59      public boolean isShuttingDown() {
60          return false;
61      }
62  
63      @Override
64      public boolean isShutdown() {
65          return false;
66      }
67  
68      @Override
69      public boolean isTerminated() {
70          return false;
71      }
72  
73      @Override
74      public boolean awaitTermination(long timeout, TimeUnit unit) {
75          return false;
76      }
77  
78      @Override
79      public void execute(Runnable command) {
80          if (command == null) {
81              throw new NullPointerException("command");
82          }
83          command.run();
84      }
85  
86      @Override
87      public <V> Promise<V> newPromise() {
88          return new ImmediatePromise<V>(this);
89      }
90  
91      @Override
92      public <V> ProgressivePromise<V> newProgressivePromise() {
93          return new ImmediateProgressivePromise<V>(this);
94      }
95  
96      static class ImmediatePromise<V> extends DefaultPromise<V> {
97          ImmediatePromise(EventExecutor executor) {
98              super(executor);
99          }
100 
101         @Override
102         protected void checkDeadLock() {
103             // No check
104         }
105     }
106 
107     static class ImmediateProgressivePromise<V> extends DefaultProgressivePromise<V> {
108         ImmediateProgressivePromise(EventExecutor executor) {
109             super(executor);
110         }
111 
112         @Override
113         protected void checkDeadLock() {
114             // No check
115         }
116     }
117 }