TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
Core Java Threads Explained
1. Core Java
Debasish Pratihari
Thread
A thread is the basic unit to which the operating
system allocates processor time.
Threads are useful to execute more or less
independent code segments concurrently of a
program.
Is knows as light-weight process.
Threads are always owned and managed by
operating system.
Every program has at least one thread.
User Thread
A user thread has a life-time of its own.
It is not depended on the thread that creates it.
It can continue execution after the thread that
created has ended.
Daemon Thread
Is a background thread that sub-ordinate to the
thread that creates it
When the thread that created the daemon
thread ends, the daemon thread dies with it.
A thread can be set to daemon before it starts
Threads
created
from
daemon
thread
are
daemon by default.
Lecture/core/thread1/20
Page #1
feel the Technology…
2. Core Java
Debasish Pratihari
Process
It has its own address space.
A process can’t access another process’s
memory .
Each process provides the resources needed to
execute a program.
Threads belongs to same process shares same
address space.
A process has at least one thread of execution.
Thread Objects:
Each thread is associated with an instance of
the class Thread
There are two ways for using thread object
Directly controlling thread
Using executor
Creating Thread
Provide a Runnable Object
class Lakshya implements Runnable{
--}
Sub class Thread
class Lakshya extends Thread{
--}
Lecture/core/thread1/20
Page #2
feel the Technology…
3. Core Java
Debasish Pratihari
Thread State:
New Born State: When we create a Thread
Object then a Thread is born.
Runnable State: When we call the start( )
method on a thread then thread is in the
Runnable state i.e., the threads in Runnable
state are all waiting in a queue for the
availability of CPU attention or their turns to
come.
Running State: When a thread got the time of
CPU then it starts execution for a particular
interval of time (time slice) this state is called as
Running state.
Blocked state: A thread is in the blocked state
when it is prevented to entered into Runnable
state.
Dead state: This is the end of life of a thread,
when a thread completes execution comes to
the dead state. Also we can dead state by
calling stop( ) method.
New
Born
Start ( )
sleep ( )
Runnable
State
Stop ( )
Scheduling
Dead
State
Lecture/core/thread1/20
Running
State
Block
State
after time-slice
or
yield( )
Page #3
feel the Technology…
4. Core Java
Debasish Pratihari
A Thread enters into blocked state under the
following situation.
By sleeping a thread.
If a thread is waiting for I/O operation.
If a thread is Interrupted.
If a thread is waiting for lock available.
Thread Life-Cycle
25%
New
Born
start( )
start ( )
notify( )/ notifyAll ( )
Runnable
State
wait lock available
I/O complete
after t time
Scheduling
mechanism
after time
slice
sleep( )
wait( )
Running
State
wait for Lock
wait for I/O
sleep( t )
B
L
O
C
K
E
D
S
T
A
T
E
Dead
State
Lecture/core/thread1/20
Page #4
feel the Technology…