Skip to main content

Top Most Important Multithreading interview question and answers



1)What is Thread in java?

  • Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application.
  • Thread are light weight process.
  • It belongs to java.lang package.
  • It can run mutiple threads simutaneously.                                                              
2) How to implements threads in java?

      By implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.

  Thread creation by implementing java.lang.Runnable interface.

    And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object 

        Thread thread = new Thread(runnable);

3)We should implement Runnable interface or extend Thread class. What are differences between implementing Runnable and extending Thread? 

               you must extend Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the best option (Runnable interface has only one abstract method i.e. run() ). 

Differences between implementing Runnable interface and extending Thread class.

1)Multiple inheritances are not allowed in java.
2)Thread Saftey.
3)Inheritance (implementing  Runnable is a lightweight operation)
4)Coding to the interface.
5) Flexibility in code when we implement Runnable 


4)When threads are not lightweight process in java?

         Threads are lightweight process only if threads of same process are executing concurrently. But if threads of different processes are executing concurrently then threads are heavy weight process.

5)How can you ensure all threads that started from main must end in order in which they started and also main should end in last?

       By using the join().  

A detailed description as follow:

          Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method with programs- 10 salient features of a join.

6)What is the difference between starting a thread with the run() and start() method? 

      When you call start() method, main thread internally calls run() method to start newly created Thread, so run() method is ultimately called by a newly created thread. 

       When you call run() method main thread rather than starting run() method with newly thread it start run() method by itself.

7)Differences between synchronized and volatile keyword in Java?

1. volatile can be used as a keyword against the variable, we cannot use volatile against method declaration.
 

volatile void method1()

      {

        } //it’s illegal, compilation error.


      volatile int i; //legal

 While synchronization can be used in method declaration or we can create synchronization blocks (In both cases thread acquires lock on object’s monitor). Variables cannot be synchronized.


Synchronized method:


synchronized void method2()

     {

       } //legal

Synchronized block:


void method2()

{
synchronized (this) 

{
//code inside synchronized block.
}
}

Synchronized variable (illegal):
synchronized int i; //it’s illegal, compilation error.

8)What is race condition in multithreading and how can we solve it? 

                 when more than one thread try to access same resource without synchronization causes race condition.
             So we can solve race condition by using either synchronized block or synchronized method. When no two threads can access same resource at a time phenomenon is also called as mutual exclusion.


9)How threads communicate between each other?

       Threads can communicate with each other by using wait(), notify() and notifyAll() methods.


10)What is deadlock?

           Deadlock is a situation where two threads are waiting for each other to release lock holded by them on resources.

 

11)what do you understand by thread starvation?

           Thread with lowest priority get less time for execution than threads with highest priority, threads with lowest priority performs a long-end during computaions, it may happen that these thread do not get eniugh time to finish their finish their computation, they seem to starve away as threads with highest priority steal their computation time.

Comments

Popular posts from this blog

Commonly asked Top 26 Java Interview Questions

1.what is a transient variable? A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword. 2. Why do threads block on I/O? Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution. 3. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4). 4. 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. Withou

MYSQL Interview question..

MySQL is an open source relational database management system RDBMS based on Structured Query Language SQL MySQL runs on virtually all platforms, including Linux, Unix, Windows. Although it can be used in a wide range of applications, MySQL is most often associated with web-based applications and online publishing and is an important component of an open source enterprise. 1) What is SQL? SQL stands for Structured Query Language. SQL was initially developed at IBM in 1970s. SQL is the standard language to communicate with relational database management systems like Oracle, MS Access, MS SQL Server, MySQL, DB2 etc. It is used to connect the objects with the database . 2)what is the Purpose of SQL? • SQL is used to Create New Databases. • SQL is used to Create New Tables in a Database. • SQL is used to Insert, update, Delete records in a Database. • SQL is used to Retrieve data from a Database. • SQL is used to execute queries against a Database. • SQL can set permis

All difference Question in java(common difference) (Collections)

1)Difference Between Interfaces And Abstract class? All the methods declared in the Interface are Abstract,where as abstract class must have atleast one abstract method and others may be concrete. In abstract class keyword abstract must be used for method, where as in Interface we need not use the keyword for methods. Abstract class must have Sub class, where as Interface can’t have sub classes.  An abstract class can extend only one class, where as an Interface can extend more than one. 2)Difference between Arrays and Vectors?             Because Vectors are dynamically-allocated, they offer a relatively memory-efficient way of handling lists whose size could change drastically. Arrays are fixed in size and it's wastage of memory problem. A Vector might be a good choice for such an arrangement. Moreover, Vectors have some useful member functions that make coding simpler. While in array is the bad choice of the arrangement and the code become more complex. Vecto