I am currently trying to learn netty-socket.io using their
demo project. I keep seeing Thread.sleep(Integer.MAX_VALUE);
. Can someone please tell me why this is important?
Addition: To clarify, I am not asking what does the Thread.sleep() function do, obviously it pauses execution on a particular thread. I am asking about the relevance of it in this example socket server.
package com.corundumstudio.socketio.demo;
import com.corundumstudio.socketio.listener.*;
import com.corundumstudio.socketio.*;
public class NamespaceChatLauncher {
public static void main(String[] args) throws InterruptedException {
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
final SocketIOServer server = new SocketIOServer(config);
final SocketIONamespace chat1namespace = server.addNamespace("/chat1");
chat1namespace.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
@Override
public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
// broadcast messages to all clients
chat1namespace.getBroadcastOperations().sendEvent("message", data);
}
});
final SocketIONamespace chat2namespace = server.addNamespace("/chat2");
chat2namespace.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
@Override
public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
// broadcast messages to all clients
chat2namespace.getBroadcastOperations().sendEvent("message", data);
}
});
server.start();
//Thread.sleep(Integer.MAX_VALUE);
Thread.sleep(4000);
server.stop();
}
}
So I figured out that this does not have anything to do with the server at all. This Thread.sleep(Integer.MAX_VALUE);
has simply paused execution of the program. To make this answer intuitive, I will change Thread.sleep(Integer.MAX_VALUE)
to Thread.sleep(4000)
in the posted code block.
ie, this would start the server, run it for 4 seconds and then stop the server.
This seems to only be here to fulfill its purpose; which is to start and stop the server, as this was taken from a demo project.