Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 3 September 2017

Java Multithreading Interview Questions and Answers

Java Multithreading Interview Questions

Java Multithreading Interview Questions

Now, Here we are gonna to discuss about java multithreading interview questions and answers. This is the most important concept which is mostly asked in core java interviews.

So let's start multithreading in java interview questions with step by step.


(1) What is the meaning of multithreading?

Multithreading means execute multiple threads simultaneously so that we can save time and handle multiple request at the same time.

There are many real time example of multithreading in java e.g washing machine, watch, etc. 

(2) What is thread in java?

Java thread is a light weight and it is a subprocess. Java thread share common address space and each thread runs in separate path.

(3) Why we use multithreading concepts in java?

We use multithreading concept in java so that we can achieve multitasking.

Multitasking: Executing multiple task is called multitasking and we perform multitasking so that we can utilize the CPU more easily.


(4) How to achieve multitasking?

We can achieve multitasking by 2 ways and these are given below.
  • Process  based multitasking(Multiprocessing)
  • Thread based multitasking(Multithreading)


(5) Difference between process and thread?

Process
  • Process is a program in execution.
  • Process is heavy weight.
  • Each process allocate separate memory area.

Thread
  • Thread is small unit or sub-process of processing.
  • Thread is light weight.
  • Thread share the same address space.


(6) State or life cycle of a thread in java?


There are five state or life cycle of java thread.

  1. New state
  2. Ready state
  3. Running state
  4. Waiting state
  5. Dead or Halted state


(7) How to create a thread?

There are two simple ways we can create thread.

  1. By extend Thread class
  2. By implement Runnable interface 

Syntax 1:  By extending Thread class


public class MyThread extends Thread{}

Syntax 2: By implement Runnable interface

public class MyThread1 implements Runnable{}


(8) Name the method of Runnable interface.

The run() method belongs to the Runnable interface.



(9) Name the some methods of Thread class.

There are lots of methods which is defined in Thread class e.g sleep(), join(), start(), isAlive(), suspend(), destroy(), resume(), stop(), etc.


(10) Can we start a thread twice?

No, We cannot start a thread twice if we will start a thread twice it will throw IllegalThreadStateException.


(11) Can we call run() method directly?

Yes, We can but it will execute like a normal object or method. It will not work like a thread.


(12) What is difference between wait() and sleep() method?

wait() method: 
  • The wait() method is defined in the Object class.
  • The wait() method allows thread to release the lock and goes to suspended state.

sleep() method
  • sleep() method belongs to the Thread class.
  • sleep() method allows the thread to go to sleep state for x milliseconds and when thread goes into sleep state it does not release the lock.



(13) What is join() method?

Until the specified thread is dead or finished execution it hold the currently running thread's execution and then join after specified thread execution is complete.


(14) What is a daemon threads?

Daemon thread is a thread that provides services to the user threads in java and it gives the background support to the user thread. Daemon thread are low priority threads. Garbage collector thread is the example of daemon thread.


(15) What is difference between notify and notifyAll()?

notify() method
  • The notify() method will run only first thread which has called wait() method on the same object.

notifyAll() method
  • The notifyAll() method will run all the thread which has called wait() method on the same object.

(16) What is deadlock?

Deadlock situation is situation when two or more threads are in waiting for each other to release the resources and cannot precede the program .

(17) What is thread synchronization in java?

Synchronization is the way to control the access of multiple threads to any shared resource so that only one thread can access at a time. It also called thread synchronization or thread-safe.

(18) What is difference between sleep() and suspend() method?

sleep() method:

  • Java sleep() method is used to converting running state to waiting state and in this case the thread is convert form waiting to running state when given time period is completed.

suspend() method: 


  • Java suspend() method is used to converting running state to waiting state but in suspend() method, the waiting state is not converted into running state automatically.

(19) What is Thread leak?

Java thread leak means when an application does not release references of the thread object and those thread do not garbage collected.

(20) How to make user thread(Thread) to daemon thread?

Yes, we can make user thread as daemon thread by using setDaemon() method.

(21) Can we change user thread to daemon thread after starting thread?

No, We can't change the user thread to daemon thread after thread starting.


(22) How many thread at a time can access a monitor?

Only one thread can access a monitor at a time.

(23) What is the name of default thread at the time of starting the program?

Name is main thread.

(24) What is the values of minimum-priority, normal-priority and maximum-priority level.

Values is 1, 5, and 10.


(25) Can we make variables and classes synchronized?

No, we cannot make variables and classes synchronized. Only methods and blocks can be synchronized in java.

Above all the multithreading concepts interview questions in java are quite useful for core java interview point of view.

Read More:

Java OOPS Interview Questions.
Java Exception Handling Interview Questions.
Struts Framework Interview Questions.
Top 10 Java Programming Interview Questions.

Share:

3 comments:

  1. Hello Anurag,

    Amaze! I have been looking bing for hours because of this and I also in the end think it is in this article! Maybe I recommend you something helps me all the time?
    In the village "Village" there are Shape bots - robots that can be shown in different shapes - triangle, rectangle, hexagon and circle. Each robot has a name, dimensions (that are assigned during the creation of Shape bot), strength (that is computed as the product of their surface and randomly generated number from 1 to 100), and the ability to transform (rectangle can transform to triangle such that it is halved by diagonal, and isosceles triangle is formed by merging two halves of a rectangle.
    All Java performance tuning techniques require some level of expertise; depending on the experience level these techniques can used.
    Two triangles can be transformed in a rectangle only if they are right angled and have equal sides.). Robots of the shape of a triangle can fly, those of the shape of a circle can swim, and rest of them can walk and fly.
    According to this, they can be on a particular position in a 40 X 20 matrix. Shape bots have their enemies - Music bots, that can destroy Shape bots by singing. They can sing only if they are on the ground - otherwise they are automatically destroyed.
    But great job man, do keep posted with the new updates.

    Muchas Gracias,
    Medhuna

    ReplyDelete
  2. I love this java multithreading post

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate