r38
r38
Threads
Question: Where does java thread support reside
Question: What happens when you call the start() method of the thread
Answer: This registers the thread with a piece of system code called thread
scheduler
The schedulers determines which thread is actually running
Answer: 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
Answer: 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: Which way would you prefer to implement threading , by extending Thread
class or implementing Runnable interface
Answer: 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
Answer: 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: Every thread has a priority, the higher priorit thread gets preference over
the lower priority thread by the thread scheduler
Answer: It is from 1 to 10. 10 beings the highest priority and 1 being the lowest
Answer: 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
Answer: 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
Answer: 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.
Answer: 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.
Answer: wait(), notify() and notifyAll() are all part of Object class and they have
to be called from synchronized code only
Answer: 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.
Answer: 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