This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

Thread PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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

Download Presentation


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript



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

With other modules of the application.


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 .

Difference between multiprocessing and 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.

Note thread class object thread are different

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:publicintgetPriority();

  • 4) setpriority():-

  • Syntax:public void setpriority();





  • 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 booleanisAlive();


  • 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();



Scheduling algo works here.

Start ();

Runnable /ready to run


Swaped states over

Control is taken back from the thread by the scheduler

Processor is alloted


Thread is suspended



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

  • Syntax:public static thread currentThread();


    Defining user thread:

    A user thread is represented by the run method , run method can be defined in the following two ways:

  • Define a sub class of thread and override run method.

  • 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


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


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


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

Inter thread communication

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;


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


Sleeping for the specified time




Block for I/O






Waiting for joined thread for complete or time out.




Waiting for lock




Wating for notification


Method or some blockesexceuted


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


  • 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();


  • 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();


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

  • Public intactiveCount();

  • 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 );

Daemon threads

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.


  • 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();

  • Login