Multithreading in JAVA Programming

13
Java Programming Multithreading in Java Programming Prof. Shailesh Gahane Assistant Professor Dr. D. Y. Patil School of MCA Charholi (Bk), Lohegaon, Pune – 412105 Mail ID: [email protected] Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

description

Multithreading 7.1 Multithreading concepts 7.2 Thread Life cycle 7.3 Creating multithreaded application (Using Thread Class & Using Runnable Interface) 7.4 Thread priorities 7.5 Thread synchronization 7.6 Inter thread communication

Transcript of Multithreading in JAVA Programming

Page 1: Multithreading in JAVA Programming

Java ProgrammingMultithreading in Java Programming

Prof. Shailesh GahaneAssistant Professor

Dr. D. Y. Patil School of MCACharholi (Bk), Lohegaon, Pune – 412105Mail ID: [email protected]

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Page 2: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Multithreading in Java Programming

• Java provides built-in support for multithreaded programming.

• A multithreaded program contains two or more parts that can run concurrently.

• Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking.

Two Types of Multitasking:1) Process Based2) Thread Based

Page 3: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

DIFFERENCE BETWEEN PROCESS BASED AND THREAD BASED MULTITASKING

Two or more than two programs execute concurrently. For eg. Typing in MS-WORD and listening the song. Where as Single program perform two or more task simultaneously. For eg. we will saving the file and order to print the file.

More Overhead compared to Thread Based Multitasking.

Running Heavyweight tasks. Eg. Run the Java compiler at the same time that you are using a text editor, Where as running the Lightweight tasks. Eg: text editor can format text at the same time that it is printing.

It has separate address space Where as Thread Based Multitasking shares the address space.

Inter-process communication is expensive and limited Where as Inter-thread communication is inexpensive. Switching between one thread to another is easy.

Page 4: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

T H R E A D S T A T E S / M O D E L A N D L I F E C Y C L E

New

Ready Running

In ActiveFinishe

d

start ()

stop()

run ()

stop ()

stop()

sleep () / suspend ()

wakeup (), notify () notify

all ()

Page 5: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Thread States or Life Cycle

New State:- When any kind of thread is newly created it enters into the new state. New state tell us that the lifetime of the thread starts and thread is considered as alive.

Ready State:- This state is also called as Ready to Run state. Mainly thread is primarily entered into this state after the start() method is invoked.

Running State:- When the thread pass from ready position to the execution that means it is entered into the Running State.

Page 6: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Inactive State:- There are several reasons for the thread to enter into the Inactive state. The meaning of this state is that the thread is entered into the non runnable state. Sleep() or wait() methods are responsible.

Finished State:- This is the final state of the thread life cycle. This state is also called as Dead State. After the completion of run() method or the stop() method is invoked then the finished state comes into the picture.

Page 7: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Thread PrioritiesThere are priorities for some of the methods which work on the thread priority.SetPriority() Priorities range from 0 to 10. MIN_PRIORITY(0), NORM_PRIORITY(5), MAX_PRIORITY(10)

GetPriority() : This method is used to get the priority of the thread.Thread Methods• IsAlive(): returns a true or false.• join(): this method waits until the thread on

which it terminates. Its name comes from the concept of calling thread waiting until the specified thread joins it.

• suspend(): pauses the execution.• resume(): restart the execution.• destroy(): kill the method.

Page 8: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

• activeCount(): returns the number of active thread in a thread group.

• interrupt(): Interrupts the thread.• isDaemon(): returns true or false and checks the

thread is Daemon or not.• yield(): used for temporarily pause and allows

further thread to execute.

Page 9: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Thread Synchronization in Java

• Java Thread Synchronization uses• the concept of monitors• the concept that every object

has a lock• Thread synchronization is divided in two

parts• Synchronized Method• Synchronized Block / Statement

Page 10: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

• Every object has a lock associated with it.• Calling a synchronized method attempts to

possess the lock– If no one uses the lock, then the thread uses the

lock.

• If any thread wants to execute the synchronized method, firstly it has to obtain the object lock. If the lock is already held by another thread, then calling thread has to wait.

• The lock is released when a thread exits the synchronized method.

Page 11: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

public synchronized void enter(Object item){

while (count == BUFFER_SIZE)Thread.yield();

++count;buffer[in] = item;in = (in + 1) % BUFFER_SIZE;

}

Example of Synchronized Method

Synchronized Block:In synchronized statements, we have to specify the object that provides the intrinsic lock.The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.The general form of Synchronized block is:Synchronized (object reference expression){Block of code;}

Page 12: Multithreading in JAVA Programming

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Example of Synchronized Block:

synchronized (this) { for(int i=0; i<5; i++) {

System.out.println("Started...."+str);

try {

Thread.sleep(5000); } catch(Exception e1)

{} } }

Example of Synchronized method:

public synchronized void display(String str)

{ for(int i=0; i<5; i++) {

System.out.println("Started...."+str);

try { Thread.sleep(5000); } catch(Exception e1){} } }}

Page 13: Multithreading in JAVA Programming

Thank you !!!

Prof. Shailesh T. Gahane, Dr. D Y Patil School of MCA, Pune

Prof. Shailesh GahaneAssistant Professor

Dr. D. Y. Patil School of MCACharholi (Bk), Lohegaon, Pune – 412105Mail ID: [email protected]