Java Reference
In-Depth Information
memory to the JVM. If the stack size is too small to store all local variables used at a time, you may encounter a
StackOverflowError
. To set the stack size for each thread, you can use a non-standard JVM option called
-Xssn
,
where
n
is the size of the thread stack. To set the stack size to 512 KB while running a
Test
class, you can use a
command, like so:
java -Xss512k com.jdojo.threads.Test
Summary
A thread is a unit of execution in a program. An instance of the
Thread
class represents a thread in Java programs.
The thread starts its execution in the
run()
method of the
Thread
class or its subclass. To execute your code in a
thread, you need to subclass the
Thread
class and override its
run()
method; you can also use an instance of the
Runnable
interface as the target for a thread. Beginning from Java 8, you can use a method reference of any method
that takes no parameters and returns
void
as the target for a thread. A thread is scheduled by using the
start()
method of the
Thread
class.
There are two types of threads: daemon and non-daemon. A non-daemon thread is also known as a user thread.
The JVM exits when only threads running are all daemon threads.
Each thread in Java has a priority that is an integer between 1 and 10, 1 being the lowest priority and 10 being the
highest priority. The priority of a thread is a hint, which can be ignored, to the operating system about its importance
for getting the CPU time.
In a multi-threaded program, a section of code that may have undesirable effects on the outcome of the program
if executed by multiple threads concurrently is called a
critical section
. You can mark a critical section in a Java
program using the keyword
synchronized
. Methods can also be declared as
synchronized
. Only one
synchronized
instance method of an object can be executed at a time by any threads. Only one
synchronized
class method of a class
can be executed at a time by any threads.
A thread in a Java program goes through a set of states that determines its life cycle. A thread can be in any one of
these states: new, runnable, blocked, waiting, timed-waiting, terminated. States are represented by constants of the
Thread.State
enum. Use the
getState()
method of the
Thread
class to get the current state of the thread.
A thread can be interrupted, stopped, suspended, and resume. A stopped thread or a thread that has finished
executing cannot be restarted.
Atomic variables, explicit locks, the synchronizer, the executor framework, and the fork/join framework are
provided as class libraries to the Java developers to assist in developing concurrent application. Atomic variables
provide variables that can be atomically updated without using explicit synchronization. Explicit locks have features
that let you acquire locks and back off if the locks are not available. The executor framework helps schedule tasks. The
fork/join framework is written on top of the executor framework to assist in working with tasks that can be divided in
subtasks and finally their results can be combined.
Thread-local variables are implemented through the
ThreadLocal
class. They store values based on threads.
They are suitable for values that are local to threads and that cannot be seen by other threads.