Skip to content

Commit

Permalink
Allow to get an Iterator over all of the EventExecutor an EventExecut…
Browse files Browse the repository at this point in the history
…orGroup contains. Beside this allow to get basic stats for the EventExecutor like pendingTasks and executorCount
  • Loading branch information
Norman Maurer authored and trustin committed Apr 9, 2013
1 parent 067a2af commit 0efebd5
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.util.concurrent;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.RunnableFuture;
Expand All @@ -30,6 +32,11 @@ public EventExecutor next() {
return this;
}

@Override
public Iterator<EventExecutor> iterator() {
return new EventExecutorIterator();
}

@Override
public <V> Promise<V> newPromise() {
return new DefaultPromise<V>(this);
Expand Down Expand Up @@ -91,4 +98,26 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialD
throw new UnsupportedOperationException();
}

private final class EventExecutorIterator implements Iterator<EventExecutor> {
private boolean nextCalled;

@Override
public boolean hasNext() {
return !nextCalled;
}

@Override
public EventExecutor next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
nextCalled = true;
return AbstractEventExecutor.this;
}

@Override
public void remove() {
throw new UnsupportedOperationException("read-only");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.netty.util.concurrent;

import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

Expand All @@ -26,13 +26,20 @@
* to shut them down in a global fashion.
*
*/
public interface EventExecutorGroup extends ScheduledExecutorService {
public interface EventExecutorGroup extends ScheduledExecutorService, Iterable<EventExecutor> {

/**
* Returns one of the {@link EventExecutor}s that belong to this group.
*/
EventExecutor next();

/**
* Returns a read-only {@link Iterator} over all {@link EventExecutor}, which are handled by this
* {@link EventExecutorGroup} at the time of invoke this method.
*/
@Override
Iterator<EventExecutor> iterator();

@Override
Future<?> submit(Runnable task);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.util.concurrent;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -75,6 +76,19 @@ public EventExecutor next() {
return children[Math.abs(childIndex.getAndIncrement() % children.length)];
}

@Override
public Iterator<EventExecutor> iterator() {
return children().iterator();
}

/**
* Return the number of {@link EventExecutor} this implementation uses. This number is the maps
* 1:1 to the threads it use.
*/
public final int executorCount() {
return children.length;
}

/**
* Return a safe-copy of all of the children of this group.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ protected boolean hasTasks() {
return !taskQueue.isEmpty();
}

/**
* Return the number of tasks that are pending for processing.
*/
public final int pendingTasks() {
return taskQueue.size();
}

/**
* Add a task to the task queue, or throws a {@link RejectedExecutionException} if this instance was shutdown
* before.
Expand Down
45 changes: 45 additions & 0 deletions common/src/main/java/io/netty/util/internal/ReadOnlyIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.netty.util.internal;

import java.util.Iterator;

public final class ReadOnlyIterator<T> implements Iterator<T> {
private final Iterator<? extends T> iterator;

public ReadOnlyIterator(Iterator<? extends T> iterator) {
if (iterator == null) {
throw new NullPointerException("iterator");
}
this.iterator = iterator;
}

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public T next() {
return iterator.next();
}

@Override
public void remove() {
throw new UnsupportedOperationException("read-only");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.netty.util.concurrent.EventExecutorGroup;

/**
* Special {@link io.netty.util.concurrent.EventExecutorGroup} which allows to register {@link Channel}'s that get
* Special {@link EventExecutorGroup} which allows to register {@link Channel}'s that get
* processed for later selection during the event loop.
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@


import io.netty.util.concurrent.AbstractEventExecutorGroup;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.ReadOnlyIterator;

import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -105,6 +108,11 @@ protected ThreadPerChannelEventLoop newChild(
return new ThreadPerChannelEventLoop(this);
}

@Override
public Iterator<EventExecutor> iterator() {
return new ReadOnlyIterator<EventExecutor>(activeChildren.iterator());
}

@Override
public EventLoop next() {
throw new UnsupportedOperationException();
Expand Down

0 comments on commit 0efebd5

Please sign in to comment.