1)What is Thread in java?
- Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application.
- Thread are light weight process.
- It belongs to java.lang package.
- It can run mutiple threads simutaneously.
2) How to implements threads in java?
By implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.
Thread creation by implementing java.lang.Runnable interface.
And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object
Thread thread = new Thread(runnable);
3)We should implement Runnable interface or extend Thread class. What are differences between implementing Runnable and extending Thread?
you must extend Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the best option (Runnable interface has only one abstract method i.e. run() ).
Differences between implementing Runnable interface and extending Thread class.
1)Multiple inheritances are not allowed in java.
2)Thread Saftey.
3)Inheritance (implementing Runnable is a lightweight operation)
4)Coding to the interface.
5) Flexibility in code when we implement Runnable
4)When threads are not lightweight process in java?
Threads are lightweight process only if threads of same process are executing concurrently. But if threads of different processes are executing concurrently then threads are heavy weight process.
5)How can you ensure all threads that started from main must end in order in which they started and also main should end in last?
By using the join().
A detailed description as follow:
Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method with programs- 10 salient features of a join.
6)What is the difference between starting a thread with the run() and start() method?
When you call start() method, main thread internally calls run() method to start newly created Thread, so run() method is ultimately called by a newly created thread.
When you call run() method main thread rather than starting run() method with newly thread it start run() method by itself.
3)We should implement Runnable interface or extend Thread class. What are differences between implementing Runnable and extending Thread?
you must extend Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the best option (Runnable interface has only one abstract method i.e. run() ).
Differences between implementing Runnable interface and extending Thread class.
1)Multiple inheritances are not allowed in java.
2)Thread Saftey.
3)Inheritance (implementing Runnable is a lightweight operation)
4)Coding to the interface.
5) Flexibility in code when we implement Runnable
4)When threads are not lightweight process in java?
Threads are lightweight process only if threads of same process are executing concurrently. But if threads of different processes are executing concurrently then threads are heavy weight process.
5)How can you ensure all threads that started from main must end in order in which they started and also main should end in last?
By using the join().
A detailed description as follow:
Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method with programs- 10 salient features of a join.
6)What is the difference between starting a thread with the run() and start() method?
When you call start() method, main thread internally calls run() method to start newly created Thread, so run() method is ultimately called by a newly created thread.
When you call run() method main thread rather than starting run() method with newly thread it start run() method by itself.
Comments
Post a Comment