Hello Friends, In this Blog Post(Core Java Interview Questions) I am going to let you know about a few more interview questions of core java related to Threads.
Core java interview questions covers all the important questions of the topics mentioned above.
These core java interview questions is a great help to remember all the important highlights and important topics from each java chapter.
These interview questions will surely help to understand a few very confusing concepts in java.
Which thread always runs in a java program by default?
It is main thread.
Why Threads are calling light-weight?
Threads are light-weight because they utilize minimum resources of the system. This means they take less memory and less processor time.
What is the difference between single-tasking and multi-tasking?
Executing only one job at a time is called single-tasking. Executing several jobs at a time is called multiple tasking. In single-tasking, processor time is wasted, but in multi-tasking, we can utilize the processor time in an optimum way.
What is the difference between ‘extends thread’ and ‘implements runnable’? which one is advantageous?
extends Thread and implements runnable – both are functionally the same. But when we write extends thread, there is no scope to extend another class, as multiple inheritances are not supported in java.
class Myclass extends Thread, AnotherClass //invalid
If we write implements runnable, then still there is a scope to extend another class.
Class Myclass extends Another AnotherClass implements Runnable //valid
This is definitely advantageous when the programmer wants to use threads and also wants to access the features of another class.
Which method is executed by the thread by default?
Public void run() method.
What is thread synchronization?
When a thread is already acting on an object, preventing any other thread from acting on the same object is called ‘Thread Synchronization’ or ‘Thread-safe’ The object on which the threads are synchronized is called ‘Synchronized object’. Thread synchronization is recommended when multiple threads are used on the same object(in multithreading).
What is the difference between the synchronized block and the synchronized keyword?
The synchronized block is useful to synchronize a block of statements. The synchronized keyword is useful to synchronize an entire method.
What is thread deadlock?
When a thread has locked an object and waiting for another object to be released by another thread, and another thread is also waiting for the first thread to release the first object, both the threads will continue waiting forever. This is called thread deadlock.
What is the difference between sleep() band wait() method?
Both the sleep() and wait() methods are used to suspend a thread execution for a specific time. When sleep() is executed inside a synchronized block, the object is still under lock. When the wait() method is executed, it breaks the synchronized block so that the object lock is removed and it is available.
Generally, sleep() is used for making a thread to wait for some time. But wait() is used in connection with notify() or notifyAll() methods in thread communication.
What is the default priority of a thread?
When a thread is created , by default its priority will be 5.
What is daemon thread?
A daemon thread is a thread that executes continuously. Daemon threads are service providers for other threads or objects. It generally provides a background processing.
What is thread life cycle?
A thread is created using a new Thread() statement and is executed by the start() method. The thread enters ‘runnable’ state and when sleep() or wait() methods are used or when the thread is blocked on I/O.
It then goes into ‘not runnable’ state. From ‘not runnable’ state, the thread comes back to the ‘runnable’ state and continues running the statements. The thread dies when it comes out of the run() method. These state transitions of a thread are called ‘life cycle of thread’.
So this was all about the Core java interview questions in this topic.
These Core java interview questions could be a great help in your college viva, academic study, and any job interview.
In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.
Hope! you would have enjoyed this post about.
Please feel free to give your important feedbacks in the comment section below.
Have a great time!