×

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 Struts Interview Questions for 2024

1) What are the Features of Struts 2? Features of Struts 2 are: Configurable MVC components POJO based actions AJAX support Integration support Various Result Types Various Tag support Theme and...

3 minutes read.

Top 30 AJAX Interview Questions for 2024

1) What is AJAX? AJAX (Asynchronous JavaScript and XML) create better, faster and interactive web-apps. It provides data transfer between web-server and browser. 2) List some advantages of AJAX Advantages are: More Interactive: Very...

3 minutes read.

Top 14 XSL Interview Questions for 2024

1) What is XSL? XSL stands for Extensible Stylesheet Language. It is used for expressing stylesheets like CSS which describes how to display an XML of given type. 2) What is XSLT? XSLT...

4 minutes read.

Top 30 Ruby on Rails Interview Questions for 2024

1) What is Ruby on Rails? It is a server-side web application framework. It is an open source ruby framework which is used for developing database-backed web applications. Here, no compilation phase is...

4 minutes read.

Top 14 Web icon Interview Questions for 2024

1) What is Web Icon? Web Icon is a symbol that is used to represent a specific action or a capability on a webpage. It is used in documents as well...

5 minutes read.

Top 14 MathML Interview Questions for 2024

1) What is MathML? MathL stands for Mathematical Markup Language. It is extended form of XML. It is used to describe mathematical and scientific notations. 2) Who is the developer of MathML? World...

3 minutes read.

Top 29 Pascal Interview Questions for 2024

1) What is Pascal? Pascal is a high-level and Algol-based language that was developed by Niklaus Wirth. It is used to develop efficient and reliable programs. 2) What are the features of Pascal? Features...

4 minutes read.

Top 30 CSS Interview Questions for 2024

1) Define the process where block elements can be centered with css. It can be centered by using margin-left and margin-right properties we can centered the block level elements. 2) Name the...

4 minutes read.

Top 25 DB2 Interview question for 2024

1) Give breif detail about DB2? DB2 is a Relational Database Management System developed by IBM. It supports Object Oriented features and non-relational structures with XML. 2) Enlist the types of datatypes...

3 minutes read.

VB.NET Interview Questions

1) What is VB.NET? VB.NET stands for Visual Basic .NET. It is an object oriented programming language. It is implemented on .NET framework and launched by Microsoft in 2001. 2) Who is...

3 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 15 PhantomJS Interview Question for 2024

1) What is PhantomJS? PhantomJS is a lightweight headless scripted browser built on WebKit. It is used for automation web page interaction. 2) Why it is called as headless browser? PhantomJS called as...

4 minutes read.

Top 13 SAS Interview Questions for 2024

1. What is SAS and its functions? SAS (Statistical Analysis System) is statistical software designed by SAS Institute in 1960 for data inspection and report writing. SAS runs on Windows, UNIX...

5 minutes read.

Top 19 Google Maps Interview Questions for 2024

1) What is Google Maps? Google Maps is a web-based service developed by Google. It contains geographical information and provides routes and information to the client. 2) What are the features provided...

5 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 16 Arduino Interview Questions for 2024

1) What is Arduino? Arduino is an open source electronic platform. It is based on easy-to-use hardware and software. It able to read input signal. It is used to write and...

2 minutes read.

Top 25 Oracle DBA Interview Question for 2024

1) What is an Oracle Instance? Database instance is a set of memory structures that manages database file. When an instance is started Oracle database allocate memory area called SGA (System...

4 minutes read.

Top 25 MongoDB interview Questions for 2024

1) Give some history about MongoDB. MongoDB came into existence when data increased exponentially on the internet. Thus, maintainence of database became very difficult in structured DB. MongoDB is a NoSQL...

3 minutes read.

Top 30 CodeIgniter interview Questions for 2024

1) What is CodeIgniter? CodeIgniter is an open source PHP framework. It is used to develop web applications and websites. It is loosely based on MVC pattern and easy to use...

4 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.