Search code examples
javasocketsnio

Java: Get Notified On Incoming Packet


I'm working on a library that is currently using standard tcp sockets. An end user of my library registers themselves as a listener, and needs to be notified if/when a relevant message comes in.

If I spawn a worker thread within my library, there is a thread-death problem where my library may prevent the application from terminating normally when all user-threads die.

Is there a way I can be notified of a new incoming tcp packet without spawning a new thread or requiring the user to poll the library?

Thanks!


Solution

  • Register the worker thread as a "daemon" to tell the JVM that it doesn't have to wait for your thread to exit.

    http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#setDaemon(boolean)