tags:

views:

178

answers:

3

Hey, I am studying for a java exam and on a past exam the lecturer asked this question and im wondering if someone can help me.

"In the context of java explain threads. Give an example of when you might use a thread. Name two java virtual machine threads."
The first two parts of the question are easy enough but the part about naming the two VM threads is really stumping me.

A: 

So, I'm not going to give you the answer. However, think about this: Java is an interpreted language. It runs a virtual machine itself (the JVM). That must be running in order for your application (thread) to run. So think about all the threads in the JVM, not just the threads for your application.

Brian
+4  A: 

A simple thread dump from a test program shows the following threads - one is the application thread, and then you have 8 other JVM threads:

Full thread dump Java HotSpot(TM) Client VM (14.0-b16 mixed mode):

"Low Memory Detector" daemon prio=6 tid=0x0aad6c00 nid=0x9c0 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"CompilerThread0" daemon prio=10 tid=0x0aad0c00 nid=0x6d4 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x0aacb400 nid=0xda4 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x0aaca000 nid=0xcc8 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x0aab7400 nid=0xba0 in Object.wait() [0x0ac2f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x029a0b18> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        - locked <0x029a0b18> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

"Reference Handler" daemon prio=10 tid=0x0aab2c00 nid=0x418 in Object.wait() [0x0abdf000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x029a0a20> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:485)
        at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
        - locked <0x029a0a20> (a java.lang.ref.Reference$Lock)

"main" prio=6 tid=0x002b6400 nid=0x3d8 runnable [0x0090f000]
   java.lang.Thread.State: RUNNABLE
        at TestClass.main(TestClass.java:8)

"VM Thread" prio=10 tid=0x0aaafc00 nid=0x184 runnable

"VM Periodic Task Thread" prio=10 tid=0x0aad9000 nid=0xc7c waiting on condition

Although, YMMV as these results are highly dependent on the JVM you are using.

Yuval A
That completely answers my question thanks.
Shane Fagan
@Shane: not entirely. Another part of an ideal answer to such an exam question should be "... but it depends on the particular JVM implementation".
Stephen C
@Stephen - you are correct, I'll add a clarification
Yuval A
A: 

jvisualvm is an excellent tool to examine a JVM at runtime. It'll tell you all sorts of interesting things.

JesperE