BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Multithreading Interview Questions and Answers

By Chaitanya Singh | Filed Under: java

Earlier I have shared 100+ core java interview questions based on various topics of core java. In this article I am gonna share interview questions based on multithreading and concurrency only. You would face multithreading questions in almost all the interviews as this is one the frequently asked topic during interviews for java professionals.

If you are new to this topic, I would recommend you to refer this tutorial (java multithreading) before going through the below set of interview questions and answers.

Interview Questions on Multithreading

Q) What is thread in Java?
A) A Thread is a concurrent unit of execution. Or in other words you can say that it is part of process that can run concurrently with other parts of the process.

Q) What is Multithreading?
A) The process of executing multiple threads simultaneously is known as multithreading. Java supports multithreading. The main advantage of multithreading is reducing CPU idle time and improving the CPU utilization. This makes the job to be completed in less time.

Q) What is the difference between thread and Process?
A) Refer this article: Thread vs Process

Q) What are the two ways of creating a thread?
A) We can create a thread by using any of the two following methods.
1) By implementing Runnable interface
2) By extending Thread class

Q) What is a Daemon thread?
A) Refer this article: Deamon thread in Java

Q) How to make a thread (user thread) to Daemon thread?
A) By calling setDaemon() method we can make a user thread to daemon thread.
Syntax:
thread.setDaemon(true);

Q) What is difference between user thread and Daemon thread?
A) By default a thread created in a program is always a user thread, however we can make it daemon by calling setDaemon(true) method, if needed. A daemon thread runs in a background and it doesn’t prevent JVM from being shutdown. Once all the user thread gets completed the JVM shutdowns without being bothered whether a daemon thread is running or not.

Q) Can we change a user thread to deamon thread by calling setDaemon() method if the thread has already been started?
No, if the thread has been started then we cannot make it daemon because it would then throw an IllegalThreadStateException

Q) Can we call run() method of Thread class?
A) We can call run() method if we want but then it would behave just like a normal method and we would not be able to take the advantage of multithreading. In general run() methods starts execution when we call start() method of a Thread class. For more details on this: Refer this article.

Q) What is deadlock?
A) A deadlock is a condition when two or more threads are in waiting for each other to release the resources that they need. For example Thread A holds a resource X and need resource Y whereas Thread B holds a resource Y and need X, in this case both threads are waiting for each other to release the resource and are in blocked condition.

Q) What is synchronization?
A) It is a technique of granting access to the shared resources in multithread environment to avoid inconsistencies in the results.

Q) What is the difference between notify() and notifyAll()?
A) notify() wakes up the first thread that called wait() on the same object, whereas the notifyAll() method wakes up all the waiting threads.

Q) What does join() method do?
A) The join() method is used to hold the execution of currently running thread until the specified thread is dead(finished execution).
Read more about join() here.

Q) Can we start a thread twice in Java?
No, once a thread is started, it can never be started again. Doing so will throw an illegalThreadStateException. For example: Refer this article.

Comments

  1. nea says

    May 16, 2016 at 7:16 AM

    plz explain runnable interface in thread with example

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap