What Java APIs create threads

Without the source code for the Java API, it’s all the same to know if the API methods create multiple threads? Are there any conventions if you are writing a Java API and creating multiple threads. This may be a very fundamental question, but it happened that arose from a discussion in which the main question was: "How do you know which Java APIs create threads and which don't?"

+4
source share
4 answers

One way to determine which libraries create new threads is to prohibit the creation of the Thread and ThreadGroup modifications in the SecurityManager . See java.lang.SecurityManager.checkAccess(Thread) Method. By implementing your own SecurityManager , you can respond to thread creation.

To answer another question: many libraries create new threads, even if you do not expect this. For example, the HTTP APIs create timers for Keep-Alives or session timeouts. Java 2D creates a signal thread. Java itself has several threads, for example. Finalizer stream AWT / Swing event dispatcher thread, etc.

+9
source

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

I have not seen a single example of other threads created by the main Java APIs, except for the Thread class, of course. :)

But, if you use other libraries, maybe they create streams, but if you do not want to profile, you can use AspectJ to register whenever a new stream is created, and the stack track what is called, so you can see what creates streams.

UPDATE:

Swing uses 4 threads, according to this post, but it also explains how you can start killing threads if necessary.

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

+1
source

Impossible to say. Actually, I don’t think that you, as a rule, really care if you are not in some limited environment. What I found is more appropriate to determine if the method is written with the expectation of running in a specific thread (AWT event dispatch thread in the case I saw). There is also no way to do this unless the code uses some kind of naming convention or is not documented.

+1
source

If you want to see active threads, just run the jvisualvm application (located in the $ JDK / bin directory) to connect to any local Java process. You can see a lot of process information, including thread names, status, and history. Get more information here .

0
source

All Articles