In computer science thread is a part of process (bole toh thread is a sub-process). Using a thread we can do multiple task at a same time.
For example in a word processor the spelling check, word count, and line count are done at the same time.
In Java thread is an object. Multiple threads can be executed at the same time.
Algorithm to create and execute a thread
1. create a class that inherit Thread class (it is a predefined class)
2. override (define) the run() method (this method is already defined in Thread class)
3. create object of subclass of Thread
4. call start() method on the object of subclass of Thread
example:
public class A extends Thread <<= ths is step-1
{
public void run() <<-- this is step-2
{
the business logic of thread shroud be written here
}
public static void main()
{
A a = new A(); <<= this is step-3
a.start(); <<= this is step-4
}
}
Fact-1:
When a java application starts the following things happens
A. The JVM creates a thread (this thread is also known as main thread)
B. The JVM invokes main method on this thread
So, internally main method is just a thread, and an each java application has at least one thread, the main thread
Fact-2:
In Java each thread has a name, priority, and group name.
The thread with higher priority will be given a chance to execute first. And Java do supports preemptive scheduling.
In preemptive scheduling a thread do following
1. acquire the processor
2. use it
3. release it
4. and repeat the process till thread is not finished
Methods of Thread class:
1. public void run(): This method keeps the business logic that will be done by the thread (bole toh thread se kya kaam karvana hai)
2. public void start(): This method is used to start a thread (
3. public void setName(String name): This method is used to provide a name for the thread. If name is null an exception will occur (means thread ka name null nahin hona chahiye)
note: by default the names of thread are Thread-0, Thread-1, and so on
4. public String getName(): This method is used to fetch name of thread
5. public void setPriority(int pri): This method is used to provide the priority of a thread. If priority is less than 1 or greater than 10 an exception will occur
(priority 1 se 10 ke beech hi ho)
note: by default the priority of a thread is 5
6. public int getPriority(): This method is used to fetch the priority of a thread
7. public static Thread currentThread(): This method is used to fetch the object of current thread. format of return data will be Thread[name,priority,group-name]
note: by default the group-name of thread is 'main'
public class MyThread extends Thread
{
@Override
public void run()
{
// fetch the name of thread
String name = super.getName();
// fetch the priority of thread
int pri = super.getPriority();
// get the object of current thread
Thread t = Thread.currentThread();
System.out.println("Name of thread is "+name+" its priority is "
+pri+" the current thread is "+t);
}
public static void main(String[] args)
{
System.out.println("main starts");
// create few objcets of MyThread
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
// provide the names for thread using setName() method
t1.setName("Amar");
t2.setName("Akbar");
t3.setName("Anthony");
// provide the priority of thread using setPriority() method
// note: by default priority of thread is 5
t1.setPriority(6);
t2.setPriority(7);
t3.setPriority(8);
// start the threads
t1.start();
t2.start();
t3.start();
System.out.println("main ends");
}
}
For example in a word processor the spelling check, word count, and line count are done at the same time.
In Java thread is an object. Multiple threads can be executed at the same time.
Algorithm to create and execute a thread
1. create a class that inherit Thread class (it is a predefined class)
2. override (define) the run() method (this method is already defined in Thread class)
3. create object of subclass of Thread
4. call start() method on the object of subclass of Thread
example:
public class A extends Thread <<= ths is step-1
{
public void run() <<-- this is step-2
{
the business logic of thread shroud be written here
}
public static void main()
{
A a = new A(); <<= this is step-3
a.start(); <<= this is step-4
}
}
Fact-1:
When a java application starts the following things happens
A. The JVM creates a thread (this thread is also known as main thread)
B. The JVM invokes main method on this thread
So, internally main method is just a thread, and an each java application has at least one thread, the main thread
Fact-2:
In Java each thread has a name, priority, and group name.
The thread with higher priority will be given a chance to execute first. And Java do supports preemptive scheduling.
In preemptive scheduling a thread do following
1. acquire the processor
2. use it
3. release it
4. and repeat the process till thread is not finished
Methods of Thread class:
1. public void run(): This method keeps the business logic that will be done by the thread (bole toh thread se kya kaam karvana hai)
2. public void start(): This method is used to start a thread (
3. public void setName(String name): This method is used to provide a name for the thread. If name is null an exception will occur (means thread ka name null nahin hona chahiye)
note: by default the names of thread are Thread-0, Thread-1, and so on
4. public String getName(): This method is used to fetch name of thread
5. public void setPriority(int pri): This method is used to provide the priority of a thread. If priority is less than 1 or greater than 10 an exception will occur
(priority 1 se 10 ke beech hi ho)
note: by default the priority of a thread is 5
6. public int getPriority(): This method is used to fetch the priority of a thread
7. public static Thread currentThread(): This method is used to fetch the object of current thread. format of return data will be Thread[name,priority,group-name]
note: by default the group-name of thread is 'main'
public class MyThread extends Thread
{
@Override
public void run()
{
// fetch the name of thread
String name = super.getName();
// fetch the priority of thread
int pri = super.getPriority();
// get the object of current thread
Thread t = Thread.currentThread();
System.out.println("Name of thread is "+name+" its priority is "
+pri+" the current thread is "+t);
}
public static void main(String[] args)
{
System.out.println("main starts");
// create few objcets of MyThread
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
// provide the names for thread using setName() method
t1.setName("Amar");
t2.setName("Akbar");
t3.setName("Anthony");
// provide the priority of thread using setPriority() method
// note: by default priority of thread is 5
t1.setPriority(6);
t2.setPriority(7);
t3.setPriority(8);
// start the threads
t1.start();
t2.start();
t3.start();
System.out.println("main ends");
}
}
Undoubtedly your blog is very informative sir but if u provide outputs along with the program it will be a lot more help.
ReplyDeletehope u consider it
Very Most Important Information Sir....Reading after this information i understand what is thread and how to work.Now I m work on thread.
ReplyDeleteInteresting facts.. very useful
ReplyDelete