Introduction
MultiTaasking Executing multiple tasks simultaneously .Example is class and student(Student Does multile tasks in class)2 categories we have
1)Process based
2)Thread based
Process based
Execuing several tasks simultaneously where each task is seperate independent process
Example: Typing java pgm and listening to music from same system and same time download file from internet
This is applicable to OS level
Thread based
This is at programatic level
1)Execuing several tasks simultaneously where each task is seperate independent part of te same program
Advantage is to increase performance by decreasing response time of the system
Whether it is process based or thread based main obective of muli tasking is to reduce response time of the system aand to improve
performance
Main important application areas of multi threading are
1)To develop multimedia graphics
2)To develop animations
3)To develop video games
4)Internally servers use multi threading concept
5)When cmp with old languages developing multi threaded applications in java is very easy because java provides in built support for mutithreading with rich API[Thread , Runnable , Thread group....]
What is a thread
Seperate flow of execution
ways
Extending thread class
Implementing interface
Extending thread class
1)Code with in run method is called job of thread
Case 1 Thread scheduler
Thread scheduler part of JVM
Responsible to schedule execution of threaded
There is no guarantee for exact o/p but we can provide several possible o/p's
Case 2
Difference b/w t.start and t.run
t.start then seperate thread will be created to do job
t.run then it will be executed as a method call by main thread ,no new thread is created here
Case 3 why we have to call start method
It is heart of multi threading as it performs all the mandatory activities like registering to thread scheduler..etc
Can we overload run method of thread clas??
yes we can but , Thread class start method always calls no arg run method other overloaded method we have to call explicitly
Class myThread extends Thread
{
}
If we start this thread then blank o/p as super class Thread run method is empty
Note
If we are not overriding run method then thread class run method will be executed which has empty implementation, hence we won't get any obj
Overriding of start method
Class myThread extends Thread
{
public void start()
{
S.o.p("start");
}
public void run()
{
s.o.p("ryn");
}
}
Class Test{
p s vm()
{
myThread t = new myThread
t.start();
S.O.P("main thread");
}
}
o/p start
main thread
Above o/p is produced by only main thread
Start method will be executed as a method call.Since run method is not called no new thread is created
It is not recomended to override start method
If we write super.start in start method then
New thread will be ceated and run method will be called
o/p is
child thread o/p is run
main thread executes start and main thread
Two threads will be running parellely;
Thread life cycle
New/born state --- mythread t = new mythread();
ready /runnable --- t.start();
If thread scheduler allocates processor then thread will enter in to running state
if in running state if thread calls yield method then it will go back to runnable/ready state
Dead state if run method completes
Note:
Try to restart the same thread then we get illegal state exception
Defining a thread by implementing a runnable interface
Class myRunnable implements runnable
{
public void run()
{
for(int i =0;i<10;i++)
{
s.o.p("child thread");
}
}
}
class threaddemo
{
p smv()
{
myRunnable r = new myRunnable();
Thread t1 = new Thread(r);
t.start();
}
}
o/p both threads will be executed simultaneously
myRunnable r = new myRunnable();
Thread t1 = new Thread();
Thread t2 = new Thread(r);
t1.start();////new thread will be created and thread class run method will be called
t1.run();//new thread will not be created and run will be called as normal method call
t2.start();//new thread will be created and runnable run method will be called
t2.run();//new thread will not be created and run of runnable will be called as normal method call
r.start();//cannot find symbol method start in class myrunnble
r.run();//my runnable run method willbe executed
Note
Recomended approach is implements runnable to create a thread
Constructors in thread class
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);
Durga's approach to defin a thread(not recomended to use)
myThread t = new myThread();
Thread t1 = new thread(t);
t1.start();
here myThread run method will be called
Thread Priority
1)Every thread has priority
2)Priority is always 1 -- 10
3)1 is min priority and 10 is max
Default priority for every thread is inheritated from parent thread
Default priority of main thread is 5
Prevent a thread execution
1)yield()
2)join()
3)sleep();
yield()
when ever we call Thread.yield then it pauses its execution and gives a chance to other threads of same priority
The Thread which is yieled , when it will get chance once again??
It depends on thread scheduler and we can't expect exactly
Complete prototype of yield method?
It is public staatic native void yield();
note
Some platforms won't provide proper support for yield method
No comments:
Post a Comment