×

Top 15 Java Multithreading Interview Questions for 2024

1) What is Multithreading in Java ?

It is a process of executing multiple threads simultaneously. It is used to achieve multitasking. It is mostly used in animation and games etc.

2) What are the advantages of Java Multithreading?

Advantages of multithreading are:
  • Saves time
  • Threads are independent
  • It doesn't affect other threads if exception occur in a single thread.
  • You can perform multiple operations at same time.

3) What is the use of Thread.start() method in Java?

Thread.start() method is used to run the Thread.run() method in a thread. Thread.start() method is also known as native method.

4) What are different states in lifecycle of Thread?

Different states in lifecycle of thread are:
  • New
  • Runnable
  • Running
  • Non-Runnable
  • Terminated

5) How can we create a Thread in Java?

Two ways to create thread in java are:
  • By extending Thread class
  • By implementing Runnable interface.

6) What are the commonly used constructors of thread class in Java?

The commonly used Constructors of thread class are:
  • Thread()
  • Thread(String name)
  • Thread(Runnable r)
  • Thread(Runnable r,String name)

7) What are the commonly used methods of thread class in Java?

The commonly used methods of thread class in java are:
  • public void run()
  • public void start()
  • public void sleep(long miliseconds)
  • public void join()
  • public void join(long miliseconds)
  • public int getPriority()
  • public int setPriority(int priority)
  • public String getName()
  • public void setName(String name)
  • public Thread currentThread()
  • public int getId()
  • public Thread.State getState()
  • public boolean isAlive()
  • public void yield()
  • public void suspend()
  • public void resume()
  • public void stop()
  • public boolean isDaemon()
  • public void setDaemon(boolean b)
  • public void interrupt()
  • public boolean isInterrupted()
  • public static boolean interrupted()

8) How can we create a thread by extending thread class in Java?

Example:
class TutorialandExample1 extends Thread  
{    
public void run(){    
System.out.println("thread is running...");    
}    
public static void main(String args[])  
{    
TutorialandExample1 t1=new TutorialandExample1();    
t1.start();    
}    
}

9) How can we create a thread by implementing Runnable interface in Java?

Example:
class TutorialandExample2 implements Runnable  
{    
public void run()  
{    
System.out.println("thread is running...");    
}    
public static void main(String args[]){    
TutorialandExample2 m1=new TutorialandExample2();    
Thread t1 =new Thread(m1);    
t1.start();    
}    
}

10) How can we start a thread twice in Java?

Example:
public class TutorialandExample3 extends Thread{    
public void run(){    
System.out.println("running...");    
}    
public static void main(String args[]){    
TutorialandExample3 t1=new TutorialandExample3();    
t1.start();    
t1.start();    
}    
}

11) What is the use of currentThread() method in Java?

It is used to returns a reference to the currently executing thread object.
class TutorialandExample4 extends Thread  
{    
public void run()  
{    
System.out.println(Thread.currentThread().getName());    
}    
public static void main(String args[]){    
TutorialandExample4 t1=new TutorialandExample4();    
TutorialandExample4 t2=new TutorialandExample4();    
t1.start();    
t2.start();    
}    
}

12) How threads can communicate with each other?

Threads can communicate with each other by using:
wait() method
notify() method
notifyAll() method

13) Is it important to acquire object lock before calling wait(), notify() and notifyAll()?

Yes, It is important to acquire object lock before calling these methods on object.

14) How can we create a Daemon thread in Java ?

Example:
public class TutorialandExample5 extends Thread {    
public void run(){    
if(Thread.currentThread().isDaemon())   
{   
System.out.println("daemon thread work");    
}    
else  
{    
System.out.println("user thread work");    
}    
}    
public static void main(String[] args){    
TutorialandExample5 t1=new TutorialandExample5();  
TutorialandExample5 t2=new TutorialandExample5();    
TutorialandExample5 t3=new TutorialandExample5();    
t1.setDaemon(true);    
t1.start();    
t2.start();    
t3.start();    
}    
}

15) What are the important methods of ThreadGroup class in Java?

Methods of ThreadGroup class in java are:
  • int activeCount()
  • int activeGroupCount()
  • void destroy()
  • String getName()
  • ThreadGroup getParent()
  • void interrupt()
  • void list()

Related Topics

Top 15 SQL Server Interview Question for 2024

1) Describe SQL Server? SQL server is a DataBase Management System developed by Microsoft. It was first introduced in year 2008 since, then many version has been introduced such as :...

2 minutes read.

Top 30 Sencha Touch Interview Questions for 2024

1) What is Sencha Touch? Sencha Touch is an UI (User Interface) JavaScript library. It is written in JavaScript. It is used to build mobile interface quickly and easily. It works...

6 minutes read.

Top 30 SQL Interview Questions for 2024

1. What is SQL? Ans. SQL stands for Structured Query Language.  It is a standard computer language for accessing, storing, and manipulating data stored in the relational database. SQL become an International Standard Organization (ISO)...

11 minutes read.

Top 15 ES6 Interview Questions for 2024

Commonly Asked ES6 Interview Questions and Answers 1) What is ES6? ES6 is a scripting language specification standardized. It is used to enable client-side scripting. The specification is influenced by programming languages (Self, Python, Perl,...

2 minutes read.

Top 15 RESTful Web Services Interview Question for 2024

1) What is REST? REST is web standards based architecture and stands for REpresentational State Transfer. It uses HTTP Protocol for data communication. Here, everything is a resource. 2) What are the HTTP methods used...

2 minutes read.

Top 28 Angular 2 Interview Questions for 2024

1) What is Angular 2? It is an open source JavaScript framework which is used to build web applications in JavaScript and HTML. It is also used to overcome obstacles encountered while...

4 minutes read.

Top 15 PouchDB Interview Questions for 2024

1) What is PouchDB? PouchDB is an in-browser database that allows applications to save data. It is an open source database. It runs in Node.js. 2) In which language PouchDB was written? PouchDB was written in JavaScript language. 3)...

2 minutes read.

Top 15 Hadoop Interview Questions for 2024

1) What is Hadoop? Hadoop is a framework which is used to store and process Big Data. It is a distributed computing platform and helps in analyzing Big Data. 2) What are the...

2 minutes read.

Top 48 C# Interview Questions for 2024

1) Write a program in C# to print "HELLO WORLD"? class Program         {          static void Main(string  args[])           {             System.Console.WriteLine(" HELLO WORLD ");             }          } 2) Write a program in C# to print "TutorialandExample" using namespace? Here, namespace is used to group related classes. Using System;    namespace  TutorialandExample      {         public class           {             public static void Main(string[]  args)             {              Console.WriteLine(" TutorialandExample ");             }          }       } 3) What are the...

8 minutes read.

Top 15 Foundation Interview Questions for 2024

1) What is Foundation? Foundation is a front-end framework that is for designing beautiful responsive websites. This framework is compatible with all types of devices and provides you with HTML, CSS...

2 minutes read.

Top 30 PyTorch Interview Questions and Answers

Q1: What is PyTorch? Answer: PyTorch is a machine learning library for the programming language Python, based on Torch library, used for application such as natural language processing. It is free...

9 minutes read.

Top 30 Python Interview Questions for 2024

1) What is Python? Python is a high level, interpreted and object-oriented programming language. It is easy to learn and supports multiple programming patterns. It is also known as powerful and versatile...

6 minutes read.

Top 15 iOS (iPhone, iPad) Interview Questions for 2024

Top 20 Most Frequently asked iOS Interview Questions and Answers 1) What is iOS? iOS is a mobile operating system developed by Apple Inc. iOS offers iPhone and iPad application development using...

3 minutes read.

Top 30 Aurelia Interview Questions and Answers

1) What is Aurelia? Aurelia is an open source UI framework which is used for web and mobile app development. This framework is focused on web standards and follows simple conventions. 2) What...

2 minutes read.

Top 14 Materialize CSS Framework Interview Questions for 2024

1) What is Materialize? Materialize is a CSS framework that contains library file. It is created with CSS, JavaScript and HTML. It helps to create attractive, reliable and functional web pages...

7 minutes read.

Top 15 CherryPy Interview Questions for 2024

1) What is CherryPy? CherryPy is a web framework used for Python. It provides a friendly interface for Http protocol for Python developer. 2) Why we use CherryPy? CherryPy has following strength: Simplicity: It...

3 minutes read.

Top 15 Euphoria Interview Questions for 2024

1) What is Euphoria? Euphoria is a programming language. It is open source, powerful and easy to learn. It stands for End-User Programming with Hierarchical Objects for Robust Interpreted Applications 2) Who...

2 minutes read.

Top 14 Material Design Lite Interview Questions for 2024

1) What is Material Design Lite? Material Design Lite is UI component Library. It is created with CSS, JavaScript and HTML. It is used for design purpose. It is developed by...

3 minutes read.

Top 15 Erlang Interview Questions for 2024

1) What is Erlang÷ Erlang is an open source programming language. It is used to build massively scalable soft real-time systems. It has runtime environment. It supports concurrency, fault tolerance and...

2 minutes read.

Top 15 RequireJs Interview Questions for 2024

1) What is RequireJs? RequireJs is a open source JavaScript library which is used to maintain dependencies between JavaScript files and modular programming. 2) What are the features of RequireJs? Following are the...

3 minutes read.