multithreading in java -...

14
Multithreading in Java By, Srinivas Reddy.S JAVA9S.com

Upload: vuongduong

Post on 06-Sep-2018

250 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Multithreading in Java

By, Srinivas Reddy.S JAVA9S.com

Page 2: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Multitasking - Multithreading

Handling multiple tasks – Multitasking Ability to initiate multiple processes – Multithreading

JAVA9S.com

Page 3: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Multithreading – Best examples Computer games are the best examples for Applying Multithreading.

JAVA9S.com

Page 4: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Threads – What are they?

public File download(String url){ // Download File code ….. return file; } Execution 1 File 1

Execution 2 File 2

Execution 3 File 3

Execution 4 File 4

1

2

3

4

Execution 1 File 1 Execution 2 File 2 Execution 3 File 3 Execution 4 File 4

When different tasks should be performed parallel, Multiple threads are needed to execute code.

Thread 1 Thread 2 Thread 3 Thread 4 Object

JAVA9S.com

Page 5: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

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

Page 6: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

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.

JAVA9S.com

Process Thread

Page 7: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

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

Page 8: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Creating Threads

Thread t = new Thread(); Thread

Just creates a Thread Object

t.start();

• 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.

public void run(){ // Code that should //be executed by thread }

When start() is invoked, the immediate code that will be executed is from run method

But the run in java.lang.Thread class has no link to Our application code. JAVA9S.com

Page 9: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

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 dt3 = new Downloader(“bb”);

Downloader dt4 = new Downloader(“aa”);

dt1.start();

dt2.start();

dt4.start();

dt3.start();

dt1 dt2 dt3 dt4

fd

fd

fd fd

JAVA9S.com

Page 10: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

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.

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. JAVA9S.com

Page 11: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Creating Threads - Constructors

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)

Runnable target String threadName ThreadGroup group

Object in which run exists Name that can be given for a thread A group into which the current thread should be part of

JAVA9S.com

Page 12: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

Creating Threads - Demonstration

JAVA9S.com

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

Page 13: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

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.

Page 14: Multithreading in Java - JAVA9S.comjava9s.com/wp-content/uploads/2013/06/Multithreading-in-Java-An... · • Even when you do not create threads and still your application runs

www.JAVA9S.com

facebook.com/java9s @java9s

JAVA9s JAVA9S.com

[email protected]