<<< Java Multithreading Interview Questions and Answers >>>





Java Multithreading - Questions & Answers

Here is the list of Java Multithreading Interview Questions. The answers are short and to the point.These questions are frequently asked by the recruiters. So we try our best to provide you the Java Multithreading interview questions. A list of frequently asked Java Multithreading interview questions and answers are given below.

«« | 1 2 3 | »»

Java Multithreading : Interview Questions

1) What is Thread?

Ans : Threads are light weight.Creating thread is simple because it requires less resource.


2) What is difference between a Thread and a Process?

Ans :The difference between a Thread and a Process are :

Thread Process
1. Threads share the address space of the process that created it. 1. Processes have their own address.
2. Threads have direct access to the data segment of its process. 2. Processes have their own copy of the data segment of the parent process.
3. Threads have almost no overhead. 3. Processes have considerable overhead.
4. New threads are easily created. 4. New processes require duplication of the parent process.


3) What is Thread in java?

Ans :In Java, "thread" means two different things:

  1. An instance of class java.lang.Thread.
  2. A thread of execution.


4) What is MultiThreading?

Ans :In Multi-Threaded programming, multiple threads are executing concurrently that improves the performance because CPU is not idle incase some thread is waiting to get some resources. Multiple threads share the heap memory, so it’s good to create multiple threads to execute some task rather than creating multiple processes.


5) What are the two ways of creating Thread?

Ans :The two ways of creating thread are :

  1. By Extending Thread
  2. By implementing Runnable interface.

Best way to create thread is by implementing Runnable interface.


6) What are the different states of a Thread's Lifecycle?

Ans : The difference states of a thread's lifecycle are:

  1. New
  2. Runnable State
  3. Running State
  4. Waited/Blocking/Sleeping State
  5. Dead State

New : When the instance of thread is created , it will be in new state.

Runnable State :Thread can be in runnable state in 2 ways.

  • When start method is invoked.
  • A thread can also be in runnable state after coming back from blocked or sleeping or waiting state.
  • Running State :If thread scheduler allocates CPU then thread will be in running state.

    Waited/Blocking/Sleeping State :Threads can be temporarly inactive for a short period of time.

    Dead State :Threads dies automatically when thread run method execution is completed and thread object will be garbage collected.


    7) What are the two types of MultiTasking?

    Ans :The two types of multitasking are:

    1. Process based multitasking
    2. Thread based multitasking


    8) What is Synchronization?

    Ans :Synchronization is the capabilility of control the access of multiple threads to any shared resource.

    It is used :

    1. To prevent thread interference.
    2. To prevent consistency problem.


    9) In how many ways we can do Synchronization?

    Ans : There are 2 ways :

    1. Synchronization Methods
    2. Synchronization Blocks


    10) What is the difference Synchronization Methods and Synchronization Blocks?

    Ans :The difference Synchronization Methods and Synchronization Blocks :

    Synchronization Methods : If we want a method of object to be accessed by single thread at a time we declare that method with synchronized keyword.

    Synchronization Blocks :

    Synchronizing few lines of code rather than complete method with the help of synchronized keyword.
    «« | 1 2 3 | »»