Index:
- What is Thread?
- Advantages of thread?
- How Threads are created in java?
- Difference between Thread class & Runnable interface?
- Lifecycle of thread (How thread travels)
1. What is Thread?
1. Any executable program is called as Thread.
2. Thread is a class in java present in java.lang package
3. Process of executing multiple threads simultaneously is
called as MultiThreading.
Consider below example to understanding thread:
Example of Multithreading |
2. Advantages of thread:
1. Threads share same memory area while process are not.
2. Threads are independents, not depends on another
3. How Threads are created in java?
There are 2 ways to creating Thread
I) by Extending Thread class and
II) by implementing Runnable interface
Let’s see one by one:
I) by Extending Thread class:
public class A extends Thread {
@Override
public void run() {
System.out.println("run
method executed..!");
}
public static void main(String[] args) {
// 1st Way to
execute
A
a1 = new A();
a1.start(); // This will trigger run()
// 2nd Way to
execute
/**
Thread A a1 = new
A();
a1.start(); */
}
}
Output:
run method executed..!
II) By implementing Runnable interface:
public class Abc implements Runnable {
public void run() {
System.out.println("run
method executed..!");
}
public static void main(String[] args) {
//1st Way to
execute
Runnable
r = new Abc();
r.run();
//2nd Way to execute
/** Abc
a1 = new Abc();
* Thread t = new Thread (a1);
* t.start();
* */
//3rd
Way to execute
/**
* new Thread(new Abc()).start();
* */
}
}
4. What is difference between Thread class and Runnable interface?
(Important question mainly asked in every interview room)
Thread class
|
Runnable interface
|
|
1.
|
We can’t extend another class after
extending Thread class
|
We can extend another
class even after implementing Runnable interface.
|
2
|
Created instance for Each thread
|
Only
one instance is created for all threads.
|
3
|
Hence Not memory efficient as creating
object for all threads
|
Memory efficient as only one instance serve to all threads.
|
4
|
Heavy weights
|
Light weight
|
5
|
Performance will be slower
|
Performance will
better and efficient.
|
6
|
Multiple
inheritance can Not achieve
|
Multiple inheritance can achieve
|
7
|
May cause Out of memory issues due to creating
multiple objects
|
May Not cause out of
memory issues.
|
8.
|
Use when multiple threads need to execute
multiple tasks.
|
Use when multiple threads (using same
object) executing same task
|
Note: In above differences, point 1, 2 and 6 are most important
which interviewer wants to hear from you.
5. Lifecycle of thread (How thread travels) :
Fig: Thread Life cycle |
Lifecycle consist of many states as shown in above diagram.
1. When we all start() method of thread it goes to
Ready/Runnable state from New state.
2. If thread scheduler allocates processor then it goes from
Runnable to Running state.
3. If we call join() method of Thread class then it goes
into Waiting state. After time expires, thread interrupt or calling of notify/
notify all method of Object class on current thread then it goes to Runnable
state from Running.
4. In this ways all methods called and goes to particular
states.
Question: Write a program to get all Thread’s states?
public class ThreadStates {
public static void main(String[] args) {
Thread.State
[ ] states
=
Thread.State.values();
for (Thread.State s : states) {
System.out.println(s);
}
}
}
Output:
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
No comments:
Post a Comment
**** Please let me know if you have any doubts. ****