02: 5 Java multithreading scenarios interview

Java interview questions & answers on multithreading scenarios to judge your experience in building applications with concurrency in mind.

Q1. Can you give some scenarios where you built concurrent systems?
A1. In a concurrent system different threads run concurrently, whereas in a distributed system different processes often in separate machines run concurrently, taking parallel processing to a new level. Learn more at 6 Distributed systems interview Q&As.

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 (aka NIO – New IO) for better throughput. Learn more at 15 Java old I/O and NIO (i.e. New I/O) interview Q&As

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. E.g. if you have configured 100 threads in the web container only 100 browser clients can connect to your web application concurrently, and the rest have to wait in the queue for a thread to become available.

Every thread introduces significant increase of memory footprint and CPU utilization via context switches if your server has lesser CPU cores than the configured threads. 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.

io-vs-nio-mult0-threading

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.

MINA-petrol-site-example

Frameworks like Apache MINA, Netty, Grizzly, and Akka provide event based and non-blocking paradigms to write concurrent applications. MINA & Netty are lower level frameworks than Akka, and have NIO (Java non-blocking New IO) as its core.

Akka is a higher level general purpose framework compared to MINA & Netty for building event-driven, scalable, and fault-tolerant applications. Akka is written in Scala, with language bindings provided for both Scala and Java. Akka uses the Actor model to hide all the thread-related code and gives you really simple and helpful interfaces to easily implement a scalable and fault-tolerant system.

Even though you need to have a good handle on writing concurrent programs in Java & interviewers like to quiz/test you on it, favor a framework like Akka as writing complex concurrent programs is not a trivial task, and you need to deal with threads, locks, race conditions & debugging. Writing concurrent programs without frameworks can be error-prone and can lead to code that is difficult to read, test, and maintain. If you are working in the BigData space, have a look at Apache Spark, which is based on Scala & Akka Toolkit.

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 hierarchical information, etc) could benefit from making these long running operations asynchronous. These tasks are performed on a separate worker thread without blocking the user. The user kicks off the generation of the long running task and then continue using the website without being blocked. Once the reports or the long running business process is completed, the outcome can be communicated to the user via emails or downloadable links by 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 continue to use the editor. So, multithreading helps you write more responsive applications.

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.

fork-and-join

Pseudo Code for Fork, Compute and Join

Here is the Java code. Many of the concurrency utilities are in the “java.util.concurrent” Java package as shown below.

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 consider Fork/join. This fork and join feature is used in Java 8 parallel stream processing with lambda expressions. Learn more: ExecutorService Vs Fork/Join & Future Vs CompletableFuture Interview Q&As

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

Configure the thread executor service via Spring JavaConfig.

Multithreading is all bout writing responsive applications, and quite often plays a major role in writing low latency applications, which is a high paying skill. Learn more: 9 Java low latency interview questions & answers with diagrams

4 More scenarios based

Advanced Java interview questions and answers on multithreading

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?

Beginner to advanced Java interview Q&As

Java supports low level interfaces/classes to higher level abstractions to implement concurrency

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. Atomicity, Visibility, and Ordering in Java multi threading

4. threadLocal Interview questions & answers

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

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


Categories Menu - Q&As, FAQs & Tutorials

Top