The main Thread - Creation and Handling
Every Java program when started,one thread begins running immediately.This is called the main Thread.The reason it is called main Thread is that it starts execution when our program begins.There are two basic characteristics of main Thread are:-
1.It is the Thread from which other threads are generated.
2.It finishes last to perform various tasks which are related to resources.
Main Thread works simply like a method ,from which new calls to Threads are generated and Threads are called in the call stack of main Thread.All called or created Threads must finish before than main Thread.It works simply as a method call.The main Thread finishes when all other Threads are returned.

NOTE:-Every Thread has its own seperate call stack to store its own variable and place mathod calls.The main method calls executes other Threads just in the same fashion as the main() method does.
Remember:-Each and every Java program when starts a main Thread.Keep it in your memory.
Main Thread is automatically created by the JVM when the program is started but the fact is you can control your main Thread too just like any other Thread.
You can craete instance of main Thread as follows:-
? you can use the currentThread() method which is public and static.
i.e.
public static Thread currentThread()
This method as the name suggests returns the reference to the Thread in which it is invoked.
Example:-
class Threadex{
public static void main(String a[]){
Thread t = Thread.currentThread();
System.out.println("Current Thread is "+t);
t.setName("Main Thread");
System.out.println("Current Thread is "+t);
}
}
in the output you will notice that the name of Thread is chnaged to Main Thread.
setName() method is used to set the name of current Thread.The other same method is getName() which returns the name of the Thread.
Suggetsed Reading:-
Ways of creating Threads
Java Thread Basics
Thread life Cycle
Remember:-Each and every Java program when starts a main Thread.Keep it in your memory.
Main Thread is automatically created by the JVM when the program is started but the fact is you can control your main Thread too just like any other Thread.
You can craete instance of main Thread as follows:-
? you can use the currentThread() method which is public and static.
i.e.
public static Thread currentThread()
This method as the name suggests returns the reference to the Thread in which it is invoked.
Example:-
class Threadex{
public static void main(String a[]){
Thread t = Thread.currentThread();
System.out.println("Current Thread is "+t);
t.setName("Main Thread");
System.out.println("Current Thread is "+t);
}
}
in the output you will notice that the name of Thread is chnaged to Main Thread.
setName() method is used to set the name of current Thread.The other same method is getName() which returns the name of the Thread.
Suggetsed Reading:-
Ways of creating Threads
Java Thread Basics
Thread life Cycle


















0 comments:
Post a Comment
Post a Comment