02: 10+ ♥♦ Java multithreading scenarios interview Q&As

Java multi-threading interview questions are very popular from beginner to experienced level candidates. Many struggle, including the so called experienced. That is why interviewers love this key area of concurrency management to assess your capabilities.

In Java, there are low level interfaces/classes to higher abstraction levels to implement concurrency

1) Implementing the Runnable or Callable interfaces & extending the Thread class.

2) Executor services with completable future objects.

3) Fork and Join framework.

4) Actor models like Akka.

Q1. Can you give some scenarios where you had used multi-threading in Java applications?

Scenario 1: Servlets are inherently multi-threaded

and each user will be using a thread from the thread pool. The number of threads are configured via the web container of the application server. Servlet 3.1 supports non-blocking I/O for better throughput.

What is wrong with the thread-per-request model?

Pre Servlet 3.1 uses thread-per-request model, which limits the number of concurrent connections to the number of concurrently running JVM threads. Every thread introduces significant increase of memory footprint and CPU utilization via context switches. Servlet 3.1 rectifies this via non-blocking I/O. Fewer threads can be used in a pool to execute the request. NIO allows you to manage multiple channels (network connections or files) using only a single (or fewer) threads.


Scenario 2: A MINA (i.e. a non-blocking I/O based) server with low level TCP protocol

to service 250+ petrol sites. The server supports the pay at the pump solution. The clients are C++ based and send fuel and credit card details to the server. A pool of reusable threads say 30 can be used to handle concurrent transactions.


Scenario 3: A Swing programmer deals with the following kinds of threads:

a) Initial threads that execute the initial application code.

b) The event dispatch thread, where all event-handling code is executed. Most code that interacts with the Swing framework must also execute on this thread.

c) Worker threads, also known as background threads, where time-consuming background tasks are executed. For example, loading an image, retrieving and caching the data, processing any time consuming logic, etc.

Scenario 4: Asynchronous processing by spawning a worker thread

An online application with a requirement to produce time consuming reports or a business process (e.g. rebalancing accounts, aggregating hierachical information, etc) could benefit from making these long running operations asynchronous. These tasks are performed on a separate worker thread. Once the reports or the long running business process is completed, the outcome can be communicated to the user via emails or asynchronously refreshing the web page via techniques known as “server push” or “client pull”. A typical example would be

a) A user makes a request for an aggregate report or a business process like rebalancing his/her portfolios.

b) The user input can be saved to a database table for a separate process to periodically pick it up and process it asynchronously.

c) The user could now continue to perform other functionality of the website without being blocked.

d) A separate process running on the same machine or different machine can periodically scan the table for any entries and produce the necessary reports or execute the relevant business process. This could be a scheduled job that runs once during off-peak or every 10 minutes. This depends on the business requirement.

e) Once the report or the process is completed, notify the user via emails or making the report available online to be downloaded.

A CountDownLatch can be used to wait on multiple threads performig different tasks. Once CountDownLatch reaches zero, the waiting threads can be released. For example 3 separate threads populating the header, body, and footer sections. The CountDownLatch starts from 3.

Asynchronous (i.e. non-blocking) processing in Java examples

1. Asynchronous processing in Java real life examples – part-1

2. Asynchronous processing in Java real life examples – part-2

Scenario 5: Writing your own editor in Java where syntax highlighting is performed on a separate thread

To maximize the performance of the application, the CPU intensive syntax highlighting can be carried out on a separate worker thread whilst the user can use the editor.

Scenario 6: Java 7 fork and join to process computation intensive algorithms on a multi-core machine

Example. numbers = {1,2,3,4,5,6,7,8,9,10}, sum = 55; process them using the fork and join feature introduced in Java 7.


Pseudo Code for Fork, Compute and Join

Here is the Java code

Here is the test class with the main method

The output

Q. Where to use fork/join as opposed to using the ExecutorService framework?

A. The Fork/Join Framework in Java 7 is designed for work that can be broken down into smaller tasks and the results of those tasks combined to produce the final result. Multicore processors are now widespread across server, desktop, and laptop hardware. They are also making their way into smaller devices, such as smartphones and tablets. Fork/Join offers serious gains for solving problems that involve recursion.

The fork/join tasks should operate as “pure” in-memory algorithms in which no I/O operations come into play. Also, communication between tasks through shared state should be avoided as much as possible, because that implies that locking might have to be performed. Ideally, tasks communicate only when one task forks another or when one task joins another.

ExecutorService continues to be a fine solution for many concurrent programming tasks, and in programming scenarios in which recursion is vital to processing power, it makes sense to use Fork/join. This fork and join feature is used in Java 8 parallel stream processing with lambda expressions.

Scenario 7: RESTful service processing the request asynchronously by spawning a new thread

Configure the thread executor service via Spring JavaConfig.

4 More scenario based

Questions and Answers on Java multi-threading

Q2. Can you give some scenarios where you used the synchronized keyword in Java?
Q3. Is a multi-threading programming all bout speed?
Q4. How will you handle the following production issues?
Q5. Can you describe the multi-threading issues due to to deadlocks, thread starvation, and thread contention?

45+ Must know Java multi threading Interview Questions & Answers:

1. 15 Java multi-threading interview Q&A for beginner to intermediate level

2. 6 popular Java multi-threading interview Q&A with diagrams and code

3. 5 Basic multi-threading interview Q&A

4. Atomicity, Visibility, and Ordering in Java multi threading

5. threadLocal Interview questions & answers

6. How a thread gets blocked or suspended in Java?

7. Differences between X and Y interview Q&A on multi-threading

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in FAQs Core Java, Multithreading
Tags: ,
3 comments on “02: 10+ ♥♦ Java multithreading scenarios interview Q&As
  1. manish paul says:

    Sir , can you please let me know what type of multi threading questions to expect for exactly 3 years java experience. Also should i expect executor framework questions or that is for more experienced level??

  2. Amish says:

    I would like to enroll for 1 month subscription. Are you running any offer at the moement?

Leave a Reply

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


600+ Java Interview Q&As ♥Free|♦FAQ

open all | close all

200+ Java Interview FAQs – Memory Joggers

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your "Career Skills"?

open all | close all