Académique Documents
Professionnel Documents
Culture Documents
--------------
Multitasking:
---------------
Process Based :
--------------
executing several task simultaneously where each task is independent of
each other
ex: a java typing in a editor,listening to songs,download a file from
net all happens in the same system simultaneously.
these sort of multi tasking applicable at OS level.
Thread Based:
--------------
executing several task simultaneously where each part is sub-part of
the same program but independent of each other is called thread based multi tasking
Purpose:
--------
To reduce cpu idle time to increase the performance and maximum
resource utilization.
Application Areas:
-------------------
Graphics
Gaming
web server and app servers..etc
-----------------------------------------------------------------------------------
---------------------------------------
Defining a Thread:
-------------------
//Thread Instantiation.
MyThread my = new MyThread();
//Starting of a Thread
my.start();
for(int i=0;i<5;i++){
System.out.println("Parent Thread");
}
}
}
Thread Scheduler:
-----------------
===================================================================================
=============================================================
t.start() vs t.run();
----------------------
t.start():
----------
if we call this a separate thread will be created that will be responsible
for executing the run() method.
Here there will be two threads.
t.run():
---------
a separate thread won't be created.
main method will call the run() its like normal method call by main thread.
only one thread will be created.
-----------------------------------------------------------------------------------
-------------------------------------------------
Importance of Thread.start():
------------------------------
Is responsible to register the thread with ThreadScheduler and all other mandatory
activities.
Hence without thread.start method there is no chance of starting a new thread in
java.
Due to this it is the heart of Multithreading.
-----------------------------------------------------------------------------------
---------------
}
}
If we dont have run() in our class by default Thread class run method will be
called which has no implementation.
Overriding of start():
------------------------
If we override start() then our start() will be executed like normal call and no
new thread wont be created which will never call run().
}
}
If we want to start method of Thread class then follow the code below.
super.start();
System.out.println("Start-Args");
}
public void run(){
System.out.println("With-Args");
}
}
class DriverThread {
public static void main(){
MyThread mt = new MyThread();
mt.start();
System.out.println("Main Thread");
}
}
===================================================================================
===================================================================================
Lifecycle of a Thread:
----------------------
--------------------------------------------------------------
IllegalThreadState:
----------------------
class DriverThread {
public static void main(){
MyThread mt = new MyThread();
mt.start();
System.out.println("Main Thread");
mt.start()
}
}
After starting a thread if we start the thread once again then we will get
java.lang.IllegalThreadStateException
===================================================================================
===================================================================================
===================
In the first approach our class always extends Thread class there is no chance of
extending any more which kills inheritance feature.
Runable(I)-->Thread-->MyThread
class DriverThread {
public static void main(){
MyRunnable mt = new MyRunnable();
Thread t = new Thread(mt);
//mt--> Target Runnable
t.start();
System.out.println("Main Thread");
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}
Case Study:
----------
MyRunnable mt = new MyRunnable();
Thread t1 = new Thread();
Thread t2 = new Thread(mt);
Case 1:
--------
t1.start();
New Thread will be created.
Default Thread class run() will be executed which has no implementation.
Case 2:
--------
t1.run();
No New Thread will be created.
Default Thread class run() will be executed which has no implementation.
Case 3:
--------
t2.start(mt);
New Thread will be created.
MyRunnable run() will be invoked and executed.
Case 4:
--------
t2.run();
MyRunnable run() will be executed which has no implementation and no new
thread.
Case 5:
--------
mt.start();
compile time exception.
-----------------------------------------------------------------------------------
-------------------
Thread Class Constructors:
-----------------------------
-----------------------------------------------------------------------------------
-----------------------------------
-----------------------------------------------------------------------------------
---------------------
Thread Priority:
---------------
Thread.MIN_PRIORITY = 1
Thread.MAX_PRIORITY =10
Thread.NORMAL_PRIORITY=5
1.yeild()
2join()
3.sleep()
yeild():
----------