Skip to main content

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).

(iii)Max-Priority-value(10).

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.

Comments

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.