Multithreading in Java
JAVA9S.com
By,
Srinivas Reddy.S
Multitasking - Multithreading
Handling multiple tasks Multitasking
Ability to initiate multiple processes Multithreading
JAVA9S.com
Multithreading Best examples
Computer games are the best examples for Applying Multithreading.
JAVA9S.com
Threads What are they?
Thread 1 Thread 2 Thread 3
public File download(String url){
// Download File code
..
return file;
}
Execution 1 File 1
Object
Thread 4
4
3
2
Execution 2 File 2
Execution 4 File 4
Execution 3 File 3
Execution 2 File 2
Execution 1 File 1
Execution 3 File 3
Execution 4 File 4
When different tasks should be performed parallel, Multiple threads are needed
to execute code.
JAVA9S.com
Thread
A thread is like the electricity passing through the wire to the devices.
A single wire or Multi wire can power the devices to run.
JAVA9S.com
Process and Threads
A single application running in OS is a process.
A process can have multiple threads.
Spell checker in Word can be considered as a thread.
Process
JAVA9S.com
Thread
Threads Few points
Thread is a thread of execution.
Even when you do not create threads and still your
application runs.. Threads are executing your application in
the background.
When main() method is called a thread known as main
thread is created to execute the program.
It is the OS which schedules the threads to be processed by
the Processor. So the scheduling behavior is dependent on
the OS.
Nothing can be guaranteed about the treads execution.
JAVA9S.com
Creating Threads
Thread t = new Thread();
Thread
Just creates a Thread Object
t.start();
When start() is invoked, the immediate code
that will be executed is from run method
To start a thread to execute the code, the start method should be invoked.
But there is no guarantee that the thread will start immediately when start is invoked.
JAVA9S.com
public void run(){
// Code that should
//be executed by thread
}
But the run in java.lang.Thread class has no link to Our application code.
Creating Threads
class Downloader extends Thread{
private String url;
public Downloader(String url){
this.url = url;
}
public void run(){
FileDownloader fd = new FileDownloader();
fd.download(this.url);
}
}
class FileDownload{
public File download(String url){
//code to download file
return file;
}
}
Downloader dt1 = new Downloader(xx);
Downloader dt2 = new Downloader(yy);
Downloader dt4 = new Downloader(aa);
Downloader dt3 = new Downloader(bb);
dt1
dt2
dt3
dt4
dt1.start();
dt2.start();
fd
fd
fd
fd
dt3.start();
dt4.start();
JAVA9S.com
Creating Threads
class Downloader extends Thread{
private String url;
public Downloader(String url){
this.url = url;
}
public void run(){
FileDownloader fd = new FileDownloader();
fd.download(this.url);
}
}
When you extend Thread, what you really mean is that you
are going to create a class more better or customized thread
than that is there in Java.
Do not use Thread class for Inheritance just to override
run() method.
Extending the Thread class in these type of cases is not a
good OO practice.
JAVA9S.com
import java.lang.Runnable;
class Downloader implements Runnable{
private String url;
public Downloader(String url){
this.url = url;
}
public void run(){
FileDownloader fd = new FileDownloader();
fd.download(this.url);
}
}
Downloader d = new Downloader();
Thread t = new Thread(d);
t.start(); //do not forget to start thread
Runnable interface insists to override run() method which is need for a thread when it starts.
By passing a Runnable Type of object to Thread, we are directing the thread to find the run
method inside the object we have passed.
Creating Threads - Constructors
Runnable target
Object in which run exists
String threadName
Name that can be given for a thread
ThreadGroup group
A group into which the current
thread should be part of
Thread()
Thread(Runnable traget)
Thread(Runnable target, String name)
Thread(String name)
Thread(ThreadGroup group, Runnable Target)
Thread(ThreadGroup group, Runnable Target, String name)
Thread(ThreadGroup group, Runnable Target, String name, long stackSize)
Thread(ThreadGroup group, String name)
JAVA9S.com
Creating Threads - Demonstration
The Tortoise and Hare story
Tortoise and Hare Join the race.
Hare sleeps in the mid of Race
thinking its too faster than tortoise.
Tortoise continues to move slowly
Without stopping and wins the race.
- Slow and Steady wins the race.
Lets implement this story to demonstrate Creating Threads
JAVA9S.com
Test yourself with Threads
Create an application which simulates the running
race of 100 meters.
The number of runners should be ten and given
name to each runner thread.
Print the winner and all the threads should
complete the race.
Print the time taken by each Runner to complete
the race and highlight the Winners time.
www.JAVA9S.com
[email protected]
facebook.com/java9s
@java9s
JAVA9s
JAVA9S.com