Multi Threading PP T
Multi Threading PP T
Team3
What is Multitasking ?
Multitasking: Executing several tasks
simultaneously is the concept of
multitasking. There are two types of
multitasking's.
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.
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
{
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