Thread A thread represents an independent module of an application that can be concurrently...

19

Click here to load reader

description

Note: thread class object & thread are different. A thread class provides various methods most of these methods are final. 1) getName():- return a name of the thread. Syntax: public String getName(); 2) setName():- it gives name to thread. Syntax:public void setName(String name); 3)getpriority():- Syntax:public int getPriority(); 4) setpriority():- Syntax:public void setpriority();

Transcript of Thread A thread represents an independent module of an application that can be concurrently...

Page 1: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

Thread A thread represents an independent module of an

application that can be concurrently executionWith other modules of the application.

MULTITHREADING

multithreading represents concurrent execution of multiple thread. Multiple thread is a light weight

version of multiprocessing is the way that less overhead incurred by the o/s in the management

to thread as compared to processes .

Page 2: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

Difference between multiprocessing and multithreading

• 1) in multiprocessing each process represents an independent application where as in multithreading each thread represents an independent module of an application .

• 2) in multithreading each process has its own address space where as in multithreading all the threads share common address space .

• Note: java.lang.Thread class provides functionality of defining a thread , starting a thread , suspending a thread , managing a thread . For each thread an object of thread class is created this object is used to manage the thread or to find out change the state of a thread.

Page 3: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

Note: thread class object & thread are different.

• A thread class provides various methods most of these methods are final.1) getName():- return a name of the thread.

• Syntax: public String getName();• 2) setName():- it gives name to thread.• Syntax:public void setName(String name);• 3)getpriority():- • Syntax:public int getPriority();• 4) setpriority():- • Syntax:public void setpriority();

Page 4: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• MAX _PRIORITY 10• MIN_PRIORITY 1• DEFAULT PRIORITY OF A USER THREAD 5• Sleep():- it is used to suspend current thread for

the specified type.• Syntax: public static void sleep(Long

milliseconds )throws Interrupted exception.• isAlive():- returns a Boolean value that is true if

thread represented by the thread object is in the memory otherwise returns false

• Syntax: public boolean isAlive();

Page 5: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• run():- represents a thread that is instruction and run method are executed by the JVM as a thread.

(it is non final method)Syntax:public void run();Start():- it is responsible for starting the execution of a

thread.Syntax: public void start();

STATES OF THREADS

Page 6: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• nweInsta

nce()

Runnable /ready to

run

running

suspended

Start ();

Swaped states overControl is taken

back from the thread by the

scheduler Processor is alloted

Thread is suspend

ed

Scheduling algo works here.

Page 7: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• CurrentThread():- return the reference of the thread object for the currenrt thread.

• Syntax:public static thread currentThread(); Pgm1Defining user thread:A user thread is represented by the run method , run method can be

defined in the following two ways:1) Define a sub class of thread and override run method.2) Define a class that implements java.lang.Runnable interface & define

its only method run().Public interface Runnable{Void run();}Note: thread class provides default implementation of runnable

interface . pgm 2

Page 8: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• Join():- join method of thread class is used to suspend the current thread till invoking thread terminates or current thread times out.

• Syntax:• public void join() throws Interruptedexception;• Public void join (long miliseconds)throws

interruptedexception;

Page 9: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• Execution of threads in java is asynchronous (can’t determine at which thread will executed at what time).

• Synchronization is desirable in multithreading to share a common resources b/w multithreads in a mutually exclusive manner.

• Synchronization is achieved with the help of monitor.

• A monitor represents an exclusive lock that is obtained on an object by thread before the synchronization method or synchronized block is executed.

Page 10: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• In java implicit locking method is used .• Synchronized keywords is provided to indicate that object

must be locked exclusively before invoking a method or block of statement.

• A synchronization keyword can be used with method signature as well as can be used within a method to specifying locking for some statement.

• Syntax: synchronized acceessmodifier returntype mathodName(types arguments)

• With the help of synchronized keyword only mutual exclusivity of a shared resource can be determine.

• Sometimes order of usage of an object has to be determine.• i.e. for example in a reader & writer problem apart from a

mutual exclusivity of the common buffer.• Order of usage by reader and writer threads has to be in a

predetermine manner.

Page 11: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

Inter thread communication

• Inter thread communication is used to gain exact control over the execution of threads .inter thread communication is facilitates with help of following methods

• 1) wait();• 2) notify();• notifyAll();• Wait():- a wait method is used to instruct the current

thread to release all the locks held by it &to get suspended until some other thread invokes noytify() or notifyAll() methods by taking the lock on the same object.

• Syntax: public void wait()throws interrupted exception;

Page 12: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• Notify():- it is used to invoke a thread that is suspended by invoking a wait method on itself .

• Syntax: public void notify();• notifyAll():- it is used to invoke all the threads

that are suspended by wait method.Note: these methods are defined in object class.

Page 13: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

Runnable

Waiting for lock

Running

Sleeping for the

specified time

Wating for notification

Waiting for joined thread for

complete or time out.

Block for I/O

1

2

3

4 4.1

3.13.2

1.0

1.1

4.2

2.0

Switch

Synchronized Method or some blockes exceuted

Page 14: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• 1. call sleep() method.• 2. I/O is performed.• 3. join() method call.• 4. wait() method.• 1.0 thread is interrupted.• 1.1 specified time is over.• 2.0 I/O operation is completed.• 3.1 joined thread completed .• 3.2 thread is interrupted.• 4.1 thread is interrupted.• 4.2 notification is received.

Page 15: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• All thread have a Boolean variable name interrupted that is used by an application programmer to interrupt a thread .

• Value of interrupt flag is continuously check while a thread is an suspended state . If it set to true thread comes out the suspended pool & through interrupted exception.

• Following methods are used to set or obtain value of interrupted flag of a thread

• Interrupt():- sets the values of interrupted flag• Public void interrupt();

Page 16: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• isInterrupted():-it is used to obtain the value of interrupted flag

• Public Boolean isInterrupted();• Interrupted():- returns the current value of interrupted

flag & reset it.• Public static boolean interrupted();

• Thread Group• Java.lang.ThreadGroup class provides method to manage

logically related threads –Commonly used methods of this class are:-

• interrupt():- interrupts all the active threads of the group.• Public void interrupt();

Page 17: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• activeCount():- returns number of active threads in the group.

• Public int activeCount();• activeThreads():-returns the reference of all the

active threads of the group.• Public Thread[] active thread();• Note: to make a thread a part of a group ,Thread

class provides following connstructor • Public Thread(ThreadGroup group, String name)• Public threadGroup(String name );

Page 18: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

Daemon Threads • It is a thread are special threads that does not

compact for resources with normal threads i.e. a daemon threads is executed only if there is no normal thread in the runnable and running state. Daemon threads are used to provide services to normal threads .

• Garbage collection in java is an example of daemon thread.

• If at any point of time only daemon threads remaining as active thread then JVM aborts them & terminates i.e. a daemon thread can’t executed its own .it needs normal thread.

Page 19: Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.

• setDaemon():- it is used to make a thread daemon thread.

• Public void setDaemon(boolean flag);• Note: setDaemon() method must be invoke before

starting a thread.• Yield():- it is used to voluntary release the control

from a thread. • Public void yield();