Java Threads
Java Threads
Vidya Zope
What is a Thread?
• Individual and separate unit of execution
that is part of a process
– multiple threads can work together to
accomplish a common goal
• Video Game example
– one thread for graphics
– one thread for user interaction
– one thread for networking
What is a Thread?
Video Game
Process
video networking
interaction
Advantages
• easier to program
– 1 thread per task
• can provide better performance
– thread only runs when needed
– no polling to decide what to do
• multiple threads can share resources
• utilize multiple processors if available
Disadvantage
• multiple threads can lead to deadlock
– much more on this later
• overhead of switching between threads
Threads Concept
Multiple
threads on
multiple
CPUs
Multiple
threads
sharing a
single CPU
6
Threads in Java
Creating threads in Java:
7
Threads in Java
Creating threads in Java:
• Extend [Link] class
– run() method must be overridden (similar to main
method of sequential program)
– run() is called when execution of the thread begins
– A thread terminates when run() returns
– start() method invokes run()
– Calling run() does not create a new thread
• Implement [Link] interface
8
States of Java Threads
• 4 separate states
– new: just created but not started
– runnable: created, started, and able to run
– blocked: created and started but unable to run
because it is waiting for some event to occur
– dead: thread has finished or been stopped
States of Java Threads
stop(),
start() end of run method
runnable
new dead
wait(), notify(),
I/O request, I/O completion,
suspend() resume()
blocked
Threads in Java
Creating threads in Java:
• Extend [Link] class
• Implement [Link] interface
Create a Runnable implementer and implement run() method.
Instantiate Thread class and pass the implementer to
the Thread, Thread has a constructor which accepts Runnable instance.
Invoke start() of Thread instance, start internally calls run() of the
implementer. Invoking start(), creates a new Thread which executes
the code written in run().
Calling run() directly doesn’t create and start a new Thread, it will run
in the same thread. To start a new line of execution, call start() on
the thread.
11
Creating Threads Example 1
class Output extends Thread {
private String toSay;
public Output(String st) {
toSay = st;
}
public void run() {
try {
for(int i=1;i<3;i++) {
[Link]([Link]().getName()+ toSay);
sleep(1000);
}
} catch(InterruptedException e) {
[Link](e);
}
}
}
Example 1 (continued)
class Program {
public static void main(String [] args) {
Output thr1 = new Output("Hello");
Output thr2 = new Output("There");
[Link]();
[Link]();
}
}
D:\javaprograms>javac [Link]
D:\javaprograms>java Program
Thread-1There
Thread-0Hello
Thread-1There
Thread-0Hello
Creating Threads Example 2
class Output implements Runnable {
private String toSay;
public Output(String st) {
toSay = st;
}
public void run() {
try {
for(;;) {
[Link](toSay);
[Link](1000);
}
} catch(InterruptedException e) {
[Link](e);
}
}
}
Example 2 (continued)
class Program {
public static void main(String [] args) {
Output out1 = new Output(“Hello”);
Output out2 = new Output(“There”);
Thread thr1 = new Thread(out1);
Thread thr2 = new Thread(out2);
[Link]();
[Link]();
}
}
public class RunnableDemo {
} D:\javaprograms>java TestSynchronization1
public void run(){ 5
100
[Link](5); 200
10
} 300
15
} 400
20
500
25
Java synchronized method
}
}
Synchronized block in java