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

Decode of the image file from hibernate and struts

Create a java file for the decoding the image Image.java package com.img.action; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.img.dao.ConnectionDAO; import com.img.pojo.Film_TablePojo; import com.img.pojo.imagePojo; import com.opensymphony.xwork2.ActionSupport; public class Show extends ActionSupport { List<Object> values = new ArrayList<Object>();  public List<Object> getValues() { return values; } public void setValues(List<Object> values) { this.values = values; } /*HttpServletRequest request;     public void setServletRequest(HttpS...

Example for Registration using hibernate integration with struts2 and spring

First create a Pojo class for the Registeration form:     1)User.java                     package com.gklife.pojo; import com.gklife.RegisterDao.RegisterDao; public class User { private int id; private String name; private String Email; private String mobilenumber; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } public String getMobilenumber() { return mobilenumber; } public void setMobilenumber(String mobilenumber) { this.mobilenumber = mobilenumber; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute(){ ...

Top java programs for fresher

1) Write a program to find a repeated word? package com.repeat; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Set; public class RepeatedWords { public static void main(String[] args)  { //Creating wordCountMap which holds words as keys and their occurrences as values                  HashMap<String, Integer> wordCountMap = new HashMap<String, Integer>();               BufferedReader reader = null;                   try         {             //Creating BufferedReader object                           reader = new BufferedReader(new FileReader("C:\\sample.txt"));         ...