Updated April 15, 2023
Introduction to Java concurrency interview questions
Part 1 – Java concurrency Interview Questions (Basic)
This first part covers basic interview questions and answers:
Java Concurrency is one of the most discussed topics in a high-level and multi-threaded programming language which means we can develop many multi-threaded programs using Java. Multi-threaded programming contains two or more parts that run simultaneously in which each part handles different tasks at the same time which increases the throughput and interactivity of the program.
Computers may have several CPU’s or they may have many cores with only one CPU. By managing all these cores and building applications based on this, will be the key to build an outstanding application which is known as concurrency. It is the ability to run many processes at the same time.
Concurrence is one of the most popular topics in Java Interview Questions. Here is a list of questions from an interview perspective that may help you crack the interview.
Q1. How to create a thread in Java?
Answer:
Java is a multi-threading language which means any Java application contains at least one thread. Although many threads are running in the background, the main is the first java thread for any Java application and we can create multiple threads from it. In General, we can create a thread in two ways. The first method is by creating a runnable interface and then creating a thread object from it. The second method is to extend the existing thread class.
Q2. What is the difference between Thread and Process?
Answer:
Process:
When a program is in execution, it is known as a process. It controls the blocks of any operation in any process. Process controls blocks that contain information about all the processes.
Thread:
Thread is a part of the process, which means a process can have many threads. Running, Ready, and blocked are the three states of any Thread.
Q3. What are the atomic operations and methods in Java Concurrency?
Answer:
Atomic operations or concurrency performs a single unit of task without involving any other operations or processes. Atomic operations are important for multi-threaded environments like Java to avoid data unpredictability.
The most commonly used atomic variable classes in Java are AtomicLong, AtomicInteger, AtomicBoolean, and AtomicReferencebin which classes represent an int, long, boolean, and object reference respectively can be updated atomically. The main methods in these classes are
- CompareAndSet()– This method returns true when it gets succeded, else returns false.
- Set()– It sets or writes the data to a memory location so that the change can also be seen by other available threads.
- Get()– It gets or accesses the value from the memory location so that the changes made by other threads are also visible.
- LazySet()– It writes the value to the memory location in a particular interval of time or maybe reordered by subsequent relevant memory operations. One such use case is nullifying references, for the collection of garbage, which can never be accessed again.
Q4. What are concurrent collection classes in Java Concurrency?
Answer:
Concurrent Collection classes support concurrency of adjustable and retrievals expected concurrency for making updates.
Major classes include CopyOnWriteArrayList, ConcurrentHashMap and CopyOnWriteArraySet
Q5. What are the differences between sleep and wait for methods in Java?
Answer:
The wait method is initialized in object class that tells the calling thread or current thread to wait until another thread invokes and is also used for inter-thread communication, while the sleep method is used to pause the execution of the current thread for a particular time and then it restarts again.
Q6. How do Traditional collections differ from Concurrent collections?
Answer:
Thread safety is the most important aspect in java which is ensured only by using Concurrent collections like ConcurrentMap and ConcurrentHashMap and we cannot have thread safety while using Traditional collections like Array, LinkedList etc. In Traditional collections if a thread is running and is another existing thread tries to add elements while running, we will get a RunTimeException which can avoided in Concurrent collections.
Q7. What is the use of CountDownLatch?
Answer:
CountDownLatch is a latch that allows one or many threads to wait until a given set of operations gets complete. Initially, a count value will be assigned to the countdownlatch. This count gets decremented each time when it calls the countdown() method.
When a countdownlatch object gets created, it initializes the number of threads it should wait for. There will be many threads with this condition. All such threads start the countdown by calling the countdownlatch() method. When the count decrements to zero, the task will start running.
Q8. What is a Cyclicbarrier in Java Concurrency?
Answer:
CyclicBarrier is also known as a synchronizer which allows all the threads to wait for each of them until they finish their given task which means until each thread reaches a common execution point the threads that completed their task wait for other threads to complete which is also known as a barrier. They don’t continue the execution without the other threads.
After a particular thread reaches an execution point they call the await() method which denotes that they have reached the execution or barrier point. This method rejects execution until a particular number of threads calls the same method which is also known as tripping the barrier.
Q9. What is the use of Exchanger in Java?
Answer:
exchanger in java is used to exchange objects containing messages between two threads. when a thread calls another thread to share messages or objects between them, it waits for the other thread to call it in return. it does not send the message until it receives a calling request from the other thread. the message is returned between the threads once both the threads receive the calling request. to perform this, first, we need to call the runner thread and then the exchange() method.
Conclusion
- Java is a high-level programming language in which we can develop many multi-threaded programs and applications
- By managing cores and building applications is the key to build an outstanding application which is known as concurrency.
- Thread and concurrency is one of the most popular topics in Java Programming.
- When a program is in execution, it is known as a process whereas a thread is a part of the process which executes based on three states: Run, Ready, and block.
Recommended Articles
This is a guide to Java concurrency interview questions. Here we discuss the introduction and basic & advanced Java concurrency interview questions respectively. You may also have a look at the following articles to learn more –