Package com.manning.nettyinaction.chapter15

Source Code of com.manning.nettyinaction.chapter15.EventLoopExamples

package com.manning.nettyinaction.chapter15;

import io.netty.channel.Channel;

import java.util.concurrent.Future;

/**
* @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
*/
public class EventLoopExamples {
    /**
     * Listing 14.1
     */
    public static void executeTaskInEventLoop() {
        Channel channel = null; // get reference to channel
        channel.eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("Run in the EventLoop");
            }
        });
    }

    /**
     * Listing 14.2
     */
    public static void executeTaskInEventLoopAndCheck() {
        Channel channel = null; // get reference to channel
        Future<?> future = channel.eventLoop().submit(new Runnable() {
            @Override
            public void run() {
                // Do something
            }
        });
        // ...
        if (future.isDone()) {
            System.out.println("Task complete");
        } else {
            System.out.println("Task not complete yet");
        }
    }

    /**
     * Listing 14.3
     */
    public static void checkIfInEventLoop() {
        Channel channel = null; // get reference to channel
        if (channel.eventLoop().inEventLoop()) {
            System.out.println("In the EventLoop");
        } else {
            System.out.println("Outside the EventLoop");
        }
    }
}
TOP

Related Classes of com.manning.nettyinaction.chapter15.EventLoopExamples

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.