2. www.SunilOS.com 2
Process
A process has a self-contained execution
environment. A process generally has a
complete, private set of basic run-time
resources; in particular, each process has its
own memory space.
Most implementations of the Java virtual
machine run as a single process.
4. www.SunilOS.com 4
Thread
Threads are sometimes called lightweight
processes. Both processes and threads provide an
execution environment, but creating a new thread
requires fewer resources than creating a new
process.
Threads exist within a process — every process
(application) has at least one thread.
Threads share the process's resources, including
memory and open files.
Each thread is associated with an instance of the
class java.lang.Thread.
6. www.SunilOS.com 6
Multitasking & Multithreading
Multitasking refers to a computer's ability to perform multiple
jobs concurrently. Multitasking means more than one
programs are running together on a single machine.
A thread is a single sequence of execution within a
program.
Multithreading refers to multiple threads of control within a
single program. Each program can run multiple threads of
control within it, e.g., Web Browser.
7. www.SunilOS.com 7
HelloWithoutThread
public class HelloWithoutThread {
String name = null; // Keeps Thread Name
public HelloWithoutThread(String threadName) {// Constructor
name = threadName;
}
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(i + " Hello Thread " + name);
}
}
}
8. www.SunilOS.com 8
TestWithoutThread
public static void main(String[] args) {
HelloWithoutThread t1 = new
HelloWithoutThread(“Raina");
HelloWithoutThread t2 = new HelloWithoutThread(“Virat");
t1.run();
t2.run();
}
11. Raktabija
Raktabija had a capability
to create another Raktbija
by his single drop of blood.
Created Raktabija will be
equally powerful, and can
consume same kind of
resources and work
concurrently.
If one Raktabija dies
another will remain active
and live.
Likewise a Thread can
create another thread.
Created threads will be
equally powerful.
Threads need same kind of
resources to be executed.
Threads work concurrently.
If one thread dies, other
will remain active in
memory.
www.SunilOS.com 11
http://en.wikipedia.org/wiki/Raktavija
12. www.SunilOS.com 12
Creating Threads
There are two ways to create a Thread object
1. Inherit Thread class.
2. Implement the Runnable.
In both cases the run() method should be
implemented
13. www.SunilOS.com 13
HelloWithThread
public class HelloWithThread extends Thread {
private String name = null;
public HelloWithThread(String threadName) {
name = threadName;
}
public void run() {
for (int i = 0; i < 500; i++) {
System.out.println(i + " Hello Thread " + name);
}
}
}
14. www.SunilOS.com 14
TestWithThread
public static void main(String[] args) {
HelloWithThread t1 = new HelloWithThread(“Raina");
HelloWithThread t2 = new HelloWithThread(“Virat");
t1.start();
t2.start();
for (int i = 0; i < 500; i++) {
System.out.println(" Main Thread ");
}
}
16. www.SunilOS.com 16
Scheduling Threads
I/O operation completes
start()
Currently executed
thread
Ready queue
•Waiting for I/O operation to be completed
•Waiting to be notified
•Sleeping
•Waiting to enter a synchronized section
Newly created
threads
What happens when
a program with a
ServerSocket calls
accept()?
17. www.SunilOS.com 17
Thread State Diagram
Alive
New Thread Dead Thread
Running
Runnable
new ThreadExample();
run() method returns
while (…) { … }
Blocked
Object.wait()
Thread.sleep()
blocking IO call
waiting on a monitor
thread.start();
19. www.SunilOS.com 19
Child of other Class -Runnable Interface
If a class is child of another class and you want to make it
Thread then use Runnable interface since Java does not
support multiple inheritance.
public class HelloThread extends Hello implements Runnable {
public void run() {
System.out.println("Hello from Thread!");
}
public static void main(String args[]) {
HelloThread runThread = new HelloThread ()
Thread th =new Thread(runThread);
th.start();
}
}
20. www.SunilOS.com 20
Thread Methods
void start()
o Creates a new thread and makes it runnable.
o This method can be called only once.
void run()
o The new thread begins its life inside this method.
void stop() (deprecated)
o The thread is being terminated.
21. www.SunilOS.com 21
Thread Methods
yield()
o Pauses the currently executing thread object and allows
other thread of same priority to be executed.
sleep(int m)/sleep(int m,int n)
o The thread is slept for m milliseconds and n
nanoseconds.
22. www.SunilOS.com 22
Multiple Threads in an Application
Each thread has its private run-time stack.
If two threads execute the same method, each will
have its own copy of the local variables the
methods uses.
However, all threads see the same dynamic
memory (heap).
Two different threads can act on the same object
and same static fields concurrently.
23. www.SunilOS.com 23
Green Thread
Native Thread model:
o the threads are scheduled by the OS
o maps a single Java thread to an OS thread
Green Thread model:
o the threads are scheduled by the Virtual Machine.
o threads are invisible to the OS.
o Green threads emulate multithreaded environments without relying
on any native OS capabilities.
o They run code in user space that manages and schedules threads
o Green threads enables Java to work in environments that do not
have native thread support.
o OS doesn't know anything about the threads used in the VM. It's up
to the VM to handle all the details.
25. www.SunilOS.com 25
Preemptive Scheduling
Preemptive scheduling – the thread scheduler
preempts (pauses) a running thread and executes
other thread as per schedule.
Non-preemptive scheduling – the scheduler never
interrupts a running thread. Other thread will get
chance only if current thread dies.
The non-preemptive scheduler relies on the
running thread to yield control of the CPU so that
other threads may execute.
26. www.SunilOS.com 26
Starvation and Livelock
A non-preemptive scheduler may cause
starvation.
Runnable threads which are in ready queue
may have to wait to be executed for a very
log time or forever.
Sometimes, starvation is also called a
livelock.
27. www.SunilOS.com 27
Time-sliced Scheduling
Time-sliced scheduling – the scheduler allocates a period of
time that each thread can use the CPU. When that amount
of time has elapsed, the scheduler preempts the thread and
switches to other thread.
Non time-sliced scheduler – the scheduler does not use
elapsed time to determine when to preempt a thread. It
uses other criteria such as priority or I/O status.
28. www.SunilOS.com 28
Java Scheduling
Java-scheduler is preemptive and based on
priority of threads.
It uses fixed-priority scheduling.
Threads are scheduled according to their
priority with respect to other threads in the
ready queue.
29. www.SunilOS.com 29
Java Scheduling
The highest priority runnable thread is
always selected for execution above lower
priority threads.
When multiple threads have equally high
priority then only one of them will be
guaranteed to be executed.
Java threads are guaranteed to be
preemptive-but not time sliced.
30. www.SunilOS.com 30
Thread Priority
Every thread has a priority.
When a thread is created, it inherits the
priority of the thread that created it.
The priority values range from 1 to 10, in
increasing priority. That means 1 is lowest
and 10 is highest.
31. www.SunilOS.com 31
Thread Priority (cont.)
The priority can be adjusted subsequently using
the setPriority(5) method.
The priority of a thread may be obtained using
getPriority().
Priority constants are defined in Thread class:
o MIN_PRIORITY=1
o MAX_PRIORITY=10
o NORM_PRIORITY=5
Thread th = new Thread(“Raina”);
o th.setPriority(4);
32. www.SunilOS.com 32
Some Notes
Thread implementation in Java is actually
based on operating system support.
Some Windows operating systems support
only 7 priority levels, so different levels in
Java may actually be mapped to the same
operating system level.
33. www.SunilOS.com 33
Daemon Threads
Daemon threads are “background” threads, that
provide services to other threads, e.g., the garbage
collection thread.
The Java VM will not exit if non-Daemon threads
are executing.
The Java VM will exit if only Daemon threads are
executing.
Daemon threads will die when the Java VM exits.
t.setDaemon(true)
38. www.SunilOS.com 38
Concurrency
An object in a program can be changed by
more than one threads concurrently.
Q: Is the order of changes that were
preformed on the object important?
40. www.SunilOS.com 40
Race Condition
When two threads are simultaneously
modifying a single object, is called race-
condition.
Both threads “race” to store their values in a
single object.
The outcome of a program is affected by the
order in which the program's threads are
allocated CPU time.
41. www.SunilOS.com 41
Deposit in an Account
Account
User1
User2
Balance = 1000
Balance = 2000
getBalance():1000
setBalance(2000)
setBalance(2000)
getBalance():1000
46. www.SunilOS.com 46
RacingCondThread
public class RacingCondThread extends Thread {
public static Account data = new Account();
String name = null;
public RacingCondThread(String name) {
this.name = name;
}
public void run() {
for(int i=0;i<5;i++){
data.deposit(name, 1000);
}
}
47. www.SunilOS.com 47
RacingCondThread
public static void main(String[] args) {
RacingCondThread t1 = new RacingCondThread(“Raina");
RacingCondThread t2 = new RacingCondThread(“Virat");
t1.start();
t2.start();
}
48. www.SunilOS.com 48
Account - synchronized
public class Account {
private int balance = 0;
public synchronized void deposit(String message, int
amount) {
int bal =getBalance() + amount;
setBalance(getBalance() + amount);
System.out.println(message + " Now Bal is " + bal);
}
//…
}
50. www.SunilOS.com 50
Synchronization types
Method
public synchronized void deposit(String message, int
amount) {….
Block
public void deposit(String message, int amount) {
synchronized (this){
int bal = getBalance() + amount;
setBalance(bal);
}
System.out.println(message + " Now Bal is " + bal);
}
51. www.SunilOS.com 51
Monitors
Each object has a “monitor” that is a token used to
determine which application thread has control of a
particular object.
Access to the object monitor is queued.
There are two monitors:
o One for synchronized instance methods called Object
Monitor.
o Other for synchronized static methods called Class
Monitor.
52. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 52