Threading Questions and Answers

37) What is the difference between process and thread?

Ans: Process is a program in execution whereas thread is a separate path of execution in a program.

38) What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined?

Ans: Multithreading is the mechanism in which more than one thread run independent of each other within the process.

wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class.

wait( ) : When a thread executes a call to wait( ) method, it surrenders the object lock and enters into a waiting state.

notify( ) or notifyAll( ) : To remove a thread from the waiting state, some other thread must make a call to notify( ) or notifyAll( ) method on the same object.

39) What is the class and interface in java to create thread and which is the most advantageous method?

Ans: Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here.

40) What are the states associated in the thread?

Ans: Thread contains ready, running, waiting and dead states.

41) What is synchronization?

Ans: Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.

42) When you will synchronize a piece of your code?

Ans: When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.

43) What is deadlock?

Ans: When two threads are waiting each other and can’t precede the program is said to be deadlock.

44) What is daemon thread and which method is used to create the daemon thread?

Ans: Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.

40. What is runnable?.

Its an Interface through which Java implements Threads.The class can extend from any class but if it implements Runnable,Threads can be used in that particular application.

41. What is preemptive and Non-preemptive Time Scheduling?.

Preemptive: Running tasks are given small portions of time to execute by using time-slicing.

Non-Preemptive: One task doesn’t give another task a chance to run until its finished or has normally yielded its time.

42. What is synchronization?.

Two or more threads trying to access the same method at the same point of time leads to synchronization.If that particular method is declared as synchronized only one thread can access it at a time. Another thread can access it only if the first thread’s task is complete.

43. What are the various thread priorities?.

(i) Min-Priority-value(1).

(ii) Normal-Priority-value(5).


44.What is Inter-Thread communication?.

To exchange information between two threads.

48.Throwable class is a sub-class of object and implements Serializable.

83. What is the superclass of exception?. Throwable.

82. What is the return type of interrupt method?. void.

3.Why do threads block on I/O?

Threads block on i/o (that is enters the waiting state) so that other threads

may execute while the i/o

Operation is performed.

5. What is synchronization and why is it important?

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.

6. Can a lock be acquired on a class?

Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object.

7. What’s new with the stop(), suspend() and resume() methods in JDK 1.2?

The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

12. What state does a thread enter when it terminates its processing?

When a thread terminates its processing, it enters the dead state.

23What is the difference between yielding and sleeping?

When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.

31. What is the difference between preemptive scheduling and time slicing?

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 thenreenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

39. When a thread blocks on I/O, what state does it enter?

A thread enters the waiting state when it blocks on I/O.

43. What is a task’s priority and how is it used in scheduling?

A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.

45. When a thread is created and started, what is its initial state?

A thread is in the ready state after it has been created and started.

53. What invokes a thread’s run() method?

After a thread is started, via its start() method or that of the Thread class, the JVM invokes the thread’s run() method when the thread is initially executed.

67. What method is invoked to cause an object to begin executing as a separate thread?

The start() method of the Thread class is invoked to cause an object to begin executing as a separate thread.

72. What is the purpose of the wait(), notify(), and notifyAll() methods?

The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object’s wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object’s notify() or notifyAll() methods.

76. What are the high-level thread states?

The high-level thread states are ready, running, waiting, and dead.

82. What is an object’s lock and which object’s have locks?

An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.

93. What happens when a thread cannot acquire a lock on an object?

If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object’s lock, it enters the waiting state until the lock becomes available.

134. What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?

When a task’s interrupt() method is executed, the task enters the ready state. The next time the task enters the running state, an InterruptedException is thrown.

149. What state is a thread in when it is executing?

An executing thread is in the running state.

170. How can a dead thread be restarted?

A dead thread cannot be restarted.

174. What are three ways in which a thread can enter the waiting state?

A thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object’s lock, or by invoking an object’s wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.

191. What method must be implemented by all threads?

All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.

194. What are synchronized methods and synchronized statements?

Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method’s object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

195. What are the two basic ways in which classes that can be run as threads may be defined?

A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.

No comments: