Repository: netty/netty
File: transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueIoHandler.java (Java)

Code before:
            }

            if (allowGrowing && strategy == eventList.capacity()) {
                //increase the size of the array as we needed the whole space for the events
                eventList.realloc(false);
            }
        } catch (Error e) {
            throw e;
        } catch (Throwable t) {
            handleLoopException(t);
        }
        return handled;
    }

    int numRegisteredChannels() {
        return numChannels;
    }

    List<Channel> registeredChannelsList() {
        LongObjectMap<DefaultKqueueIoRegistration> ch = registrations;
        if (ch.isEmpty()) {
            return Collections.emptyList();
        }

        List<Channel> channels = new ArrayList<>(ch.size());

        for (DefaultKqueueIoRegistration registration : ch.values()) {
            if (registration.handle instanceof AbstractKQueueChannel.AbstractKQueueUnsafe) {
                channels.add(((AbstractKQueueChannel.AbstractKQueueUnsafe) registration.handle).channel());
            }
        }
        return Collections.unmodifiableList(channels);
    }

    private static void handleLoopException(Throwable t) {
        logger.warn("Unexpected exception in the selector loop.", t);

        // Prevent possible consecutive immediate failures that lead to
        // excessive CPU consumption.
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore.
        }
    }

    @Override
    public void prepareToDestroy() {
        try {
            kqueueWaitNow();
        } catch (IOException e) {

Review comment (suggestion):
Can also be written like this

```suggestion
        DefaultKqueueIoRegistration cancelledRegistration;
        while ((cancelledRegistration = cancelledRegistrations.poll()) != null) {
```

Code after:
            if (strategy > 0) {
                handled = strategy;
                processReady(strategy);
            }

            if (allowGrowing && strategy == eventList.capacity()) {
                //increase the size of the array as we needed the whole space for the events
                eventList.realloc(false);
            }
        } catch (Error e) {
            throw e;
        } catch (Throwable t) {
            handleLoopException(t);
        } finally {
            processCancelledRegistrations();
        }
        return handled;
    }

    // Process all previous cannceld registrations and remove them from the registration map.
    private void processCancelledRegistrations() {
        for (;;) {
            DefaultKqueueIoRegistration cancelledRegistration = cancelledRegistrations.poll();
            if (cancelledRegistration == null) {
                return;
            }
            DefaultKqueueIoRegistration removed = registrations.remove(cancelledRegistration.id);
            assert removed == cancelledRegistration;
            if (removed.isHandleForChannel()) {
                numChannels--;
            }
        }
    }

    int numRegisteredChannels() {
        return numChannels;
    }

    List<Channel> registeredChannelsList() {
        LongObjectMap<DefaultKqueueIoRegistration> ch = registrations;
        if (ch.isEmpty()) {
            return Collections.emptyList();
        }

        List<Channel> channels = new ArrayList<>(ch.size());

        for (DefaultKqueueIoRegistration registration : ch.values()) {
            if (registration.handle instanceof AbstractKQueueChannel.AbstractKQueueUnsafe) {
                channels.add(((AbstractKQueueChannel.AbstractKQueueUnsafe) registration.handle).channel());
            }
        }