A thread is an independent path of execution in a system. The high-level thread states are ready, running, waiting and dead.
All IT and Non IT Interview Questions, MCQs, Online Quiz Questions, Engineering VIVA Questions
Home » All posts
Tuesday, 14 February 2012
What is thread? What are the high-level thread states? | Java Threads
A thread is an independent path of execution in a system. The high-level thread states are ready, running, waiting and dead.
What is the purpose of the wait(), notify(), and notifyAll() methods? | Java Threads
The wait(), notify() and notifyAll() methods are used to provide an efficient way for thread inter-communication.
What invokes a threads run() method? | Java Threads
After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.
What is the difference between preemptive scheduling and time slicing? | Java Threads
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then re-enters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
How to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? | Java Threads
You have two ways to do so. First, making your class "extends" Thread class. The other way is making your class implement "Runnable" interface. The latter is more advantageous, cause when you are going for multiple inheritance, then only interface can help. . If you are already inheriting a different class, then you have to go for Runnable Interface. Otherwise you can extend Thread class. Also, if you are implementing interface, it means you have to implement all methods in the interface. Both Thread class and Runnable interface are provided for convenience and use them as per the requirement. But if you are not extending any class, better extend Thread class as it will save few lines of coding. Otherwise performance wise, there is no distinguishable difference. A thread is in the ready state after it has been created and started.
What is mutual exclusion? How can you take care of mutual exclusion using Java threads? | Java Threads
Mutual exclusion is a phenomenon where no two processes can access critical regions of memory at the same time. Using Java multithreading we can arrive at mutual exclusion. For mutual exclusion, you can simply use the synchronized keyword and explicitly or implicitly provide an Object, any Object, to synchronize on. The synchronized keyword can be applied to a class, to a method, or to a block of code. There are several methods in Java used for communicating mutually exclusive threads such as wait( ), notify( ), or notifyAll( ). For example, the notifyAll( ) method wakes up all threads that are in the wait list of an object.
What is the difference between yielding and sleeping? | Java Threads
When a task invokes its yield() method, it returns to the ready state, either from waiting, running or after its creation. When a task invokes its sleep() method, it returns to the waiting state from a running state.
Subscribe to:
Posts (Atom)