Without having the source code for a Java API, is there anyway to know if the API methods create multiple threads ? Are there any conventions to follow if you are writing Java APIs and they create multiple threads. This may be very fundamental question but it happened to spawn out of a discussion in which the crux question was – ” How do you know which Java APIs create threads and which don’t ” ?
Share
One way of determining which libraries create new threads is by disallowing
Threadcreation andThreadGroupmodification in theSecurityManager. See thejava.lang.SecurityManager.checkAccess(Thread)method. By implementing your ownSecurityManager, you are able to react on the creation of Threads.To answer the other question: many libraries create new threads, even if you don’t expect it. For example APIs for HTTP communication create Timers for Keep-Alives or session timeouts. Java 2D is creating a signalling thread. Java itself has multiple threads, e.g. the Finalizer thread; the AWT/Swing event dispatcher thread etc.