0% found this document useful (0 votes)
4 views105 pages

Multi Threading PP T

The document provides an overview of multithreading and multitasking, explaining the differences between process-based and thread-based multitasking. It covers concepts such as thread creation, thread scheduling, and the importance of using the Runnable interface over extending the Thread class. Additionally, it discusses thread management methods like yield, join, sleep, and synchronization, along with their advantages and disadvantages.

Uploaded by

Anita Maan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views105 pages

Multi Threading PP T

The document provides an overview of multithreading and multitasking, explaining the differences between process-based and thread-based multitasking. It covers concepts such as thread creation, thread scheduling, and the importance of using the Runnable interface over extending the Thread class. Additionally, it discusses thread management methods like yield, join, sleep, and synchronization, along with their advantages and disadvantages.

Uploaded by

Anita Maan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 105

Multithreading

Team3
What is Multitasking ?
 Multitasking: Executing several tasks
simultaneously is the concept of
multitasking. There are two types of
multitasking's.

1. Process based multitasking.


 2. Thread based multitasking
What is multiprocessing
 Executing several tasks simultaneously where
each task is a separate independent process
such type of multitasking is called process based
multitasking.
 Example:  While typing a java program in the
editor we can able to listen mp3 audio songs at
the same time we can download a file from the
net all these tasks are independent of each other
and executing simultaneously and hence it is
Process based multitasking.
  This type of multitasking is best suitable at "os
level".
What is multithreading ?
 Executing several tasks simultaneously
where each task is a separate independent
part of the same program, is called Thread
based multitasking. And each independent
part is called a "Thread".
The main important application
areas of multithreading are:
 1. To implement multimedia graphics
 2. To develop animations.
 3. To develop video games etc.
 4. To develop web and application servers
How can define a Thread .
 1. By extending Thread class.
 2. By implementing Runnable

interface
By using Thread()
By implementing Runnable
What is Thread Scheduler:
 If
multiple Threads are waiting to
execute then which Thread will
execute 1st is decided by
"Thread Scheduler" which is part
of JVM.
Difference between t.start() and
t.run() methods.
 In the case of t.start() a new Thread will be
created which is responsible for the
execution of run() method.

  But in the case of t.run() no new Thread


will be created and run() method will be
executed just like a normal method by the
main Thread.
: If we are not overriding run()
method:
 class MyThread extends Thread {
 }
 class ThreadDemo
 {
 public static void main(String[] args)
 {
 MyThread t=new MyThread();
 t.start();
 }
 }
 Output :
 If we are not overriding run() method then Thread class run()
method will be executed which has empty implementation
and hence we won't get any output.
Overloding of run()
method.
 class MyThread extends Thread
 {
 public void run()
 {
 System.out.println("no arg method");
 }
 public void run(int i)
 {
 System.out.println("int arg method");
 }
 }
 class ThreadDemo
 {
 public static void main(String[] args) {
 MyThread t=new MyThread();
 t.start();
 }
 }
 Output: No arg method

 We can overload run() method but Thread class start() method always
invokes no argument run() method the other overload run() methods we
have to call explicitly then only it will be executed just like normal method.
overriding of start()
method
Life cycle of thread
IllegalThreadStateException
when it raises
Which is one is the best practice for
thread Thread class or Runnable interface
 Among the 2 ways of defining a Thread,
implements Runnable approach is always
recommended.
  In the 1st approach our class should always
extends Thread class there is no chance of
extending any other class hence we are missing
the benefits of inheritance.
  But in the 2nd approach while implementing
Runnable interface we can extend some other
class also. Hence implements Runnable
mechanism is recommended to define a Thread.
Thread Constructor()
 1. Thread t=new Thread();
 2. Thread t=new Thread(Runnable r);
 3. Thread t=new Thread(String name);
 4. Thread t=new Thread(Runnable r,String name);
 5. Thread t=new Thread(ThreadGroup g,String
name);
 6. Thread t=new Thread(ThreadGroup g,Runnable
r);
 7. Thread t=new Thread(ThreadGroup g,Runnable
r,String name);
 8. Thread t=new Thread(ThreadGroup g,Runnable
r,String name,long stackSize);
Methods: 1. public final String
getName()
2. public final void setName(String
name)
 class MyThread extends Thread {

 }

 class ThreadDemo
 {

 public static void main(String[] args) {


 System.out.println(Thread.currentThread().getName());
 //main

 MyThread t=new MyThread(); System.out.println(t.getName());


 //Thread-0

 Thread.currentThread().setName("Bhaskar Thread");
 System.out.println(Thread.currentThread().getName());
 //Bhaskar Thread } }
Thread Priority constant
 Thread class defines the following constants
to represent some standard priorities.

 1. Thread. MIN_PRIORITY----------1
 2. Thread. MAX_PRIORITY----------10
 3. Thread. NORM_PRIORITY--------5

Valid Range of priority 1 to 10


1 least
10 Highest
The Methods to Prevent a
Thread from Execution:
1. yield(); Shopping
Mall
2. join();
 3. sleep();
yield():
 1. yield() method causes "to pause current
executing Thread for giving the chance of
remaining waiting Threads of same priority".
 2. If all waiting Threads have the low priority
or if there is no waiting Threads then the same
Thread will be continued its execution.
 3. If several waiting Threads with same priority
available then we can't expect exact which
Thread will get chance for execution.
 4. The Thread which is yielded when it get
chance once again for execution is depends on
mercy of the Thread scheduler.
 5. public static native void yield();
Join():
 If a Thread wants to wait until completing
some other Thread then we should go for
join() method.
 Example: If a Thread t1 executes t2.join()

then t1 should go for waiting state until


completing t2.
 1. public final void join()throws
InterruptedException

 2. public final void join(long ms) throws


InterruptedException

 3. public final void join(long ms,int ns)


throws InterruptedException
Sleep() method:
 If a Thread don't want to perform any
operation for a particular amount of time
then we should go for sleep() method.

 1. public static native void sleep(long ms)


throws InterruptedException

 2. public static void sleep(long ms,int


ns)throws InterruptedException
 public class SleepMethod {

 public static void main(String[] args)throws


InterruptedException
 {
 System.out.println("M");
 Thread.sleep(3000);
 System.out.println("E");
 Thread.sleep(3000);
 System.out.println("G");
 Thread.sleep(3000);
 System.out.println("A");
 }
 }
Interrupting a Thread:
 How a Thread can interrupt another
thread ? If a Thread can interrupt a sleeping
or waiting Thread by using interrupt()(break
off) method of Thread class. public void
interrupt();
Synchronization
 1. Synchronized is the keyword applicable for methods
and blocks but not for classes and variables.

 2. If a method or block declared as the synchronized


then at a time only one Thread is allow to execute that
method or block on the given object.

 3. The main advantage of synchronized keyword is we


can resolve date inconsistency problems.
 4. But the main disadvantage of synchronized keyword
is it increases waiting time of the Thread and effects
performance of the system.
 5. Hence if there is no specific requirement then
never recommended to use synchronized
keyword.

 6. Internally synchronization concept is


implemented by using lock concept.

 7. Every object in java has a unique lock.


Whenever we are using synchronized keyword
then only lock concept will come into the picture.
 8. If a Thread wants to execute any synchronized
method on the given object 1st it has to get the lock of
that object. Once a Thread got the lock of that object
then it's allow to execute any synchronized method on
that object. If the synchronized method execution
completes then automatically Thread releases lock.

 9. While a Thread executing any synchronized method


the remaining Threads are not allowed execute any
synchronized method on that object simultaneously. But
remaining Threads are allowed to execute any non-
synchronized method simultaneously. [lock concept is
implemented based on object but not based on method].
 Conclusion : If multiple threads are
operating on multiple objects then there is
no impact of Syncronization. If multiple
threads are operating on same java objects
then syncronized concept is
required(applicable).
 1.Explain about synchronized keyword and its
advantages and disadvantages?
 2. What is object lock and when a Thread required?
 3. What is class level lock and when a Thread
required?
 4. What is the difference between object lock and
class level lock?
 5. While a Thread executing a synchronized
method on the given object is the remaining
Threads are allowed to execute other synchronized
methods simultaneously on the same object? Ans:
No.
 6. What is synchronized block and explain its
declaration? 7. What is the advantage of
synchronized block over synchronized method?
 8. Is a Thread can hold more than one lock at a
time? Ans: Yes, up course from different objects.
Example:

You might also like