views:

139

answers:

4

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 " ?

A: 

In my experience, if you are looking at core java, not J2EE, the only time I can think that threads are created in core Java is with Swing.

I haven't seen any example of other threads being created by the core Java APIs, except for the Thread class, of course. :)

But, if you are using other libraries then it may be that they are creating threads, but, if you don't want to profile, you may want to use AspectJ to log whenever a new thread is created, and the stack track of what called it, so you can see what is creating the threads.

UPDATE:

Swing uses 4 threads, according to this post, but he also explains how you can go about killing off the threads, if needed.

http://www.herongyang.com/Swing/jframe_2.html

James Black
As far as I know, Swing (actually, AWT) only creates a single event dispatch thread, which you access through `SwingUtilities`'s `invokeLater` and `invokeAndWait` methods.
R. Bemrose
@R. Bemrose - I am not certain if Swing creates more than one, it seemed that it doesn't but that is why I put "may", as I am not 100% certain.
James Black
in the debugger of eclipse I can see at least 3 Threads ("AWT-EventQueue", "AWT-Windows", "AWT-Shutdown") when opening a single JFrame. Somtimes I also see a "DestroyJavaVM" thread, only if using AWT/Swing.
Carlos Heuberger
+8  A: 

One way of determining which libraries create new threads is by disallowing Thread creation and ThreadGroup modification in the SecurityManager. See the java.lang.SecurityManager.checkAccess(Thread) method. By implementing your own SecurityManager, 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.

mhaller
+1  A: 

There's no way to tell. Actually, I don't think you normally would care that much unless you're in some kind of constrained environment. What's I've found is more relevant is to determine if a method is written with an expectation of being run on a particular thread (the AWT Event dispatch thread, in the case I've seen). There's not a way to do that either, unless the code is using some kind of naming convention, or it's documented.

ykaganovich
A: 

If you want to see active threads, just fire up the jvisualvm application (located in your $JDK/bin directory) to connect to any local java process. You'll be able to see a multitude of information about the process, including thread names, status, and history. Get more information here.

Jason Nichols