Question: Where does java thread support reside
Answer: It resides in three places
The java.lang.Thread class (Most of the support resides here)
The java.lang.Object class
The java language and virtual machine
Question: What is the difference between Thread and a Process
Answer: Threads run inside process and they share data.
One process can have multiple threads, if the process is killed all the threads inside it are killed, they dont share data
Question: What happens when you call the start() method of the thread
This registers the thread with a piece of system code called thread scheduler
The schedulers determines which thread is actually running
Question: Does calling start () method of the thread causes it to run
No it merely makes it eligible to run. The thread still has to wait for the CPU time along with the other threads, then at some time in future, the scheduler will permit the thread to run
Question: When the thread gets to execute, what does it execute
The thread executes a method call run(). It can execute run() method of either of the two choices given below :
The thread can execute it own run() method.
The thread can execute the run() method of some other objects
For the first case you need to subclass the Thread class and give your subclass a run() method
For the second method you need to have a class implement the interface runnable. Define your run method. Pass this object as an argument to the Thread constructor
Question: How many methods are declared in the interface runnable
The runnable method declares only one method :
public void run();
Question: Which way would you prefer to implement threading , by extending Thread class or implementing Runnable interface
The preferred way will be to use Interface Runnable, because by subclassing the Thread class you have single inheritance i.e you wont be able to extend any other class
Question: What happens when the run() method returns
When the run() method returns, the thread has finished its task and is considered dead. You can't restart a dead thread. You can call the methods of dead thread
Question: What are the different states of the thread
Answer: They are as follows:
Running: The state that all thread aspire to be
Various waiting states : Waiting, Sleeping, Suspended and Bloacked
Ready : Waiting only for the CPU
Dead : All done
Question: What is Thread priority
Every thread has a priority, the higher priorit thread gets preference over the lower priority thread by the thread scheduler
Question: What is the range of priority integer
It is from 1 to 10. 10 beings the highest priority and 1 being the lowest
Question: What is the default priority of the thread
The default priority is 5
Question: What happens when you call Thread.yield()
It caused the currently executing thread to move to the ready state if the scheduler is willing to run any other thread in place of the yielding thread.
Yield is a static method of class Thread
Question: What is the advantage of yielding
It allows a time consuming thread to permit other threads to execute
Question: What happens when you call Thread.sleep()
It passes time without doing anything and without using the CPU. A call to sleep method requests the currently executing thread to cease executing for a specified amount of time.
Question: Does the thread method start executing as soon as the sleep time is over
No, after the specified time is over the thread enters into ready state and will only execute when the scheduler allows it to do so.
Question: What do you mean by thread blocking
If a method needs to wait an indeterminable amount of time until some I/O occurrence takes place, then a thread executing that method should graciously step out of the Running state. All java I/O methods behave this way. A thread that has graciously stepped out in this way is said to be blocked.
Question: What threading related methods are there in object class
wait(), notify() and notifyAll() are all part of Object class and they have to be called from synchronized code only
Question: What is preemptive scheduling
In preemptive scheduling there are only two ways for the thread to leave the running state without ecplicitly calling wait() or suspended()
It can cease t be ready to execute ()by calling a blocking I/O method). It can get moved out by CPU by a higher priorit thread that becomes ready to execute
Question: What is non-preemptive or Time sliced or round robin scheduling
With time slicing the thread is allowd to execute for a limited amount of time. It is then moved to ready state, where it must contend with all the other ready threads.
Question: What are the two ways of synchronizing the code
Synchronizing an entire method by putting the synchronized modifier in the methods declaration. To execute the method, a thread must acquire the lock of the object that owns the method.
Synchronize a subset of a method by surrounding the desired lines of code with curly brackets and inserting the synchronized expression before the opening curly. This allows you to synchronize the block on the lock of any object at all, not necessarily the object that owns the code
Question: What happens when the wait() method is called
The calling thread gives up CPU
The calling thread gives up the lock
The calling thread goes into the monitor's waiting pool
Question: What happens when the notify() method is called
One thread gets moved out of monitors waiting pool and into the ready state
The thread that was notified ust reacquire the monitors locl before it can proceed
Question: Using notify () method how you can specify which thread should be notified
Answer: You cannot specify which thread is to be notified, hence it is always better to call notifyAll() method
Ask Questions? Discuss: Core Java Interview Question, Interview Question
Post your Comment
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.