Top 150+ Java Interview Questions and Answers

    58 Votes

141. What is the basic concept of multitasking and it's types?

Multitasking is the ability to execute more than one task at the same time. Multitasking can be divided into two categories:
  • Process based multitasking
  • Thread based multitasking

 

142. Give example and describe process and thread based multitasking?
When we work with two different programs simultaneously, it is known as Process based multitasking. For example, listening to a music and working with MS-Excel.
When a program executes various tasks within simultaneously, it is known as Thread based multitasking. For example, MS-Word can do spell check and count the number of lines while we work on a document.
143. What are the disadvantages of multitasking?
  • Race condition
  • Deadlock condition
  • Lock starvation

 

144. How to use threads in java?

By using Thread class, Java.lang.Thread class is used to construct and access individual thread in a multi threaded application.

145. What are the stages of Thread Lifecycle?

  • New : It is created by instantiating the Thread class. Thread th = new Thread(this);
  • Runnable: By calling the start method that in turn call run method. th.start();
  • Not Runnable: Calling wait or sleep method or caused due to io interrupts. wait(); th.sleep(1000);
  • Dead or terminate: After run() method completes execution.

146. What are the two ways of creating threads in java?

Two ways of creating Thread in Java:

  • Extending from java.lang.Thread class
  • Implementing the Runnable Interface

147. Which is the first thread to be executed in a multithreaded process?

The first thread to be executed in a multi-threaded process is called the main thread. The main thread is created automatically on the startup of Java program execution.

148. What is thread scheduling and what does it determines?

The scheduling can be described as the management of execution of various threads on a single CPU.

  • It determines which runnable threads to run
  • It can be based on thread priority
  • It is part of OS or Java Virtual Machine (JVM)

149. What thread scheduling does java runtime supports?

The Java runtime supports fixed priority scheduling algorithm, which is simple and deterministic.

150. Describe thread policy in java?

  • Thread priorities are the integers in the range of 1 to 10 that specify the priority of one thread with respect to the priority of another thread.
  • Each Java thread is allocated with a priority number ranging between MIN_PRIORITY and MAX_PRIORITY.
  • The JRE chooses the runnable thread with the maximum priority of execution, when it had to choose from a range of threads which are ready to execute.
  • The Java Run-time Environment executes threads based on their priority.
  • A thread with higher priority runs before threads with low priority. 
  • At any moment of time, a given thread may give up its right to execute by calling the yield method.
  • You can set the thread priority after it is created using the setPriority() method declared in the Thread class.

151. What is synchronization?

Synchronization of threads makes sure that a shared resource can be accessed by only one thread at a time, even if multiple threads what to access the shared resource at any given moment of time.

152. Why synchronization is required?

  • Problem: Race condition! Two or more threads want to access the same object and each thread want to modify the state of the object. 
  • Result: Corrupted object. The solution to this is synchronization.

153. What is monitor?

A monitor is an object that can block threads and notify them when it is available.The monitor controls the way in which synchronized method's access an object or class. To enter an object’s monitor, you need to call a synchronized method.

154. How to acheive synchronization in java?

Synchronization can be achieved in Java using synchronized keyword.

Page 13 of 14

Popular Videos

communication

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers