Java Threads: Difference between revisions
(Created page with "=Internal= * java") |
|||
(21 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[java#Subjects|java]] | * [[java#Subjects|java]] | ||
=Subjects= | |||
* [[java Thread States]] | |||
* [[java ThreadLocal]] | |||
=Concepts= | |||
==Daemon Thread== | |||
{{External|http://www.javaperspective.com/daemon-threads.html}} | |||
A daemon thread is a thread that '''DOES NOT''' prevent the JVM from exiting, even if the thread is in WAITING or TIMED_WAITING state. Specifically the JVM will exit when only daemon threads remain. | |||
When the JVM halts any remaining daemon threads are abandoned: finally blocks are not executed, stacks are not unwound - JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O. | |||
When a new thread is created it inherits the daemon status of its parent. Or it can be set with setDaemon(true). | |||
For more details on the relationship between non-daemon threads and JVM shutdown, see: | |||
{{Internal|Java_Shutdown#Non-Daemon_Threads_and_.22Natural.22_Shutdown|Non-Daemon Threads and "Natural" Shutdown}} | |||
==Java Threads and Linux Processes== | |||
On a Linux system, each JVM thread counts as a "process", in that it is subject to system-wide and user-wide process number limitations - see "[[Linux_Process_Management_Concepts#Maximum_Number_of_Processes_Allowed_on_the_System|maximum number of processes allowed on a system]]" and "[[Linux_Process_Management_Concepts#Maximum_Number_of_Processes_Available_to_a_Single_User|maximum number of processes available to a single user]]". When a JVM instance adjust its number of threads, the change is reflected by <tt>/proc/stat</tt> "[[Linux_Process_Information#.2Fproc.2Fstat|processes]]" count. | |||
The number of Java threads for a JVM is reported by [[Linux_Process_Information#Threads|/proc/<pid>/status]]. | |||
For an example of what happens when the number of Linux processes allowed to an user is reached, see {{Internal|Max_User_Processes_and_Java_OutOfMemoryError_%22unable_to_create_new_native_thread%22|OutOfMemoryError "unable to create new native thread"}} | |||
A simple Java program that allows experimenting with threads: {{External|https://github.com/NovaOrdis/playground/tree/master/java/threads/multiple-threads-spinning}} | |||
==Java Thread Stack Memory Management== | |||
{{Internal|Java_Memory_Concepts#Thread_Stack_Memory_Management|Thread Stack Memory Management}} | |||
==Difference between Blocked and Waiting== | |||
{{External|http://geekexplains.blogspot.com/2008/07/threadstate-in-java-blocked-vs-waiting.html}} | |||
==Thread Dump== | |||
A ''thread dump'' is the result of one invocation of the [[jstack]] command, or of sending [[Linux_Signals#SIGQUIT_.283.29|SIGQUIT]] (-3) into the Java virtual machine process. A thread dump consists in a set of [[#Stack_Trace|stack traces]], one stack trace for each thread in the Java virtual machine. | |||
==Stack Trace== |
Latest revision as of 01:31, 22 July 2017
Internal
Subjects
Concepts
Daemon Thread
A daemon thread is a thread that DOES NOT prevent the JVM from exiting, even if the thread is in WAITING or TIMED_WAITING state. Specifically the JVM will exit when only daemon threads remain.
When the JVM halts any remaining daemon threads are abandoned: finally blocks are not executed, stacks are not unwound - JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.
When a new thread is created it inherits the daemon status of its parent. Or it can be set with setDaemon(true).
For more details on the relationship between non-daemon threads and JVM shutdown, see:
Java Threads and Linux Processes
On a Linux system, each JVM thread counts as a "process", in that it is subject to system-wide and user-wide process number limitations - see "maximum number of processes allowed on a system" and "maximum number of processes available to a single user". When a JVM instance adjust its number of threads, the change is reflected by /proc/stat "processes" count.
The number of Java threads for a JVM is reported by /proc/<pid>/status.
For an example of what happens when the number of Linux processes allowed to an user is reached, see
A simple Java program that allows experimenting with threads:
Java Thread Stack Memory Management
Difference between Blocked and Waiting
Thread Dump
A thread dump is the result of one invocation of the jstack command, or of sending SIGQUIT (-3) into the Java virtual machine process. A thread dump consists in a set of stack traces, one stack trace for each thread in the Java virtual machine.