THREAD:
Thread is the lightweight Process(uses Shared Memory of application).
States:
Mansur, [01.10.18 09:35]
Thread is a lightweight (uses the shared memory of application) process (program in execution in waiting state).
Multiprogramming execution of more than one application (vlc and notepad execute at a time)
Multithreading executes more than one threads in a single application. (in NFS 4 cars are executing at a time)
States of threads:
New state->ready .->running/waiting->dead state
To create thread:
1.Extending Thread Class
it contains start() method.
2.BY implementing Runnable Interface .
Start () method is present in the Thread class. It is used to create Thread. Whenever the Start method of Thread class is called it registers the thread into Thread Scheduler and calls the run method.
Recommendation:
-
we should not override start method in implementing class or extending class otherwise it will become normal method.
-
Start method of thread class calls run method
Run method :
it is available in runnable interface which is overriden by Thread class Run method.Run method is called by start method of Thread Class.
:Recommendation:
we should override the run method in our class to get the desired output of thread.
class Mythread extends Thread
{
public void run ()
{
for(int i=0;i<10 font="" i="">10>
System.out.println(i+" "+Thread.currentThread().getName());
}
}
class A
{
public static void main(String[] args) {
Mythread t=new Mythread();
t.start();
for(int i=100;i<110 font="" i="">110>
System.out.println(Thread.currentThread().getName()+i);
}
}
the output of the thread is decided by the thread Scheduler based on the algorithms.
1.Pre-emptive Algorithms.
2.Time-Slicing Algorithms.
On the basis of priority, the order of execution of the thread is decided.
Priority Varies from 1(MIN_PRIORITY) to 10(MAX_PRIORITY).
5 is NORM_PRIORITY. It is default priority of the main thread.
All the thread started by the main thread will also have default priority 5.
Less priority means the execution of that thread will be later.
Whenever we override the start method, the thread will not be created.
Generally, we use Runnable interface to use Threading in Program because the interface has multiple inheritances allowed...if we extends Thread class then we will not be able to extends other class however if we use Interface we can still have other class and interface to extends or implements.
class B
{
public static void main(String[] args) {
new Thread(new Thread(){public void run (){
for(int i=0;i<10 font="" getname="" i="" system.out.println="">10>
System.out.println("Hello java "+
getName());
}}).start();
new Thread(new Thread(){public void run (){
for(int i=0;i<10 font="" getname="" i="" system.out.println="">10>
System.out.println("Bye java "+getName());
}}).start();
System.out.println("main "+Thread.currentThread());
}
}
some methods of Thread class:
setName(): to set the name of Thread. Example : t.setName(“Beast Thread”);
getName(): to get the Thread name.Example: t.getnName();
setPriority(int ): to set the priority of the thread.
getPriority() : get the priorirty of the Thread.
Daemon Thread :
-
this process are those process which run in background.
-
daemon thread stops if main thread stops.
The function of Daemon Thread:
setDaemon(boolean): it is used to make the normal thread a Daemon.
Ex: st.etDaemon(true); will make the thread as a daemon thread.
class demon extends Thread
{
public void run ()
{
for (int i=0;i<20 i="">20>
System.out.println("user defined Thread with deamon enabled "+i);
try{
Thread.sleep(1);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class B
{
public static void main(String[] args) {
demon t=new demon();
t.setDaemon(true);// now the program terminates of the main thread stops.
//t.setDaemon(false); it will make the thread t to full run.
t.start();
for (int i=0;i<10 i="">10>
System.out.println("Main thread "+i);
}
}
}
_____________________________some Excpetion related methods_________________________
toString() : when we call toString the exception type and reason also comes.
getmessage():gives the exception only,it prints only the message part of the output printed by the object.
printStackTrace() : gives the whole Stack Trace of Exception.
By default, printStackTrace is called in the case of Exception.
Hook thread will be executed just before the termination of the JVM normally or abnormally.
Hook thread used to have the cleanup code.
Creating Hook thread:
Runtime.getRuntime().addShutdownHook(Thread obj);
Example:
class ShutDownHook
{
public static void main(String[]
args) {
Runtime.getRuntime().addShutdownHook( new Thread()
{
public void run()
{
System.out.println( "Shutdown
Hook is running !" );
}
});
System.out.println( "Application
Terminating ..." );
}
}
|
class demon extends Thread
{
public void run ()
{
for (int i=0;i<20 i="">20>
System.out.println("user defined Thread with deamon enabled "+i);
try{
Thread.sleep(1);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class B
{
public static void main(String[] args) {
demon t=new demon();
t.setDaemon(true);
t.start();
for (int i=0;i<10 i="">10>
System.out.println("Main thread "+i);
}
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){ System.out.println("shuttind down");
}
});
}
}
Comments
Post a Comment
share your thoughts ....