4.3 Java MultiThreading
4.3 Java MultiThreading
Java threads
Threads in Programming Languages
if (!t.isAlive()) {
t.start();
}
The getState() method
The possible thread states are
NEW: A thread that hasn’t been started yet
RUNNABLE: The thread can be run, and may be running
as we speak
It may not because another runnable thread could be running
BLOCKED: The thread is blocked on a monitor
RUNNABLE
BLOCKED/
NEW not WAITING/
running running TIMED_WAITING
TERMINATED
Thread Lifecycle: 4 states
RUNNABLE
start() BLOCKED/
NEW not WAITING/
running running TIMED_WAITING
TERMINATED
Thread Lifecycle: 4 states
sleep()
block on I/O
wait()
RUNNABLE
start() BLOCKED/
NEW not WAITING/
running running TIMED_WAITING
TERMINATED
Thread Lifecycle: 4 states
sleep()
block on I/O
wait()
RUNNABLE
start() BLOCKED/
NEW not WAITING/
running running TIMED_WAITING
time elapsed
I/O done
notify()
TERMINATED
Thread Lifecycle: 4 states
sleep()
block on I/O
wait()
RUNNABLE
start() BLOCKED/
NEW not WAITING/
running running TIMED_WAITING
TERMINATED
So what?
At this point, it seems that we throw a bunch of threads in,
and we don’t really know what happens
To some extent it’s true, but we have ways to have some
control
In particular, what happens in the RUNNABLE state?
RUNNABLE
not
running running