Java Concurrency Interview Questions


  1. What is atomic operation? What are atomic classes in Java Concurrency API?

    Atomic operations are performed in a single unit of task without interference from other operations. Atomic operations are necessity in multi-threaded environment to avoid data inconsistency.
    int++ is not an atomic operation. So by the time one threads read it’s value and increment it by one, other thread has read the older value leading to wrong result.
    To solve this issue, we will have to make sure that increment operation on count is atomic, we can do that using Synchronization but Java 5 java.util.concurrent.atomic provides wrapper classes for int and long that can be used to achieve this atomically without usage of Synchronization. Go to this article to learn more about atomic concurrent classes.
  2. What is Lock interface in Java Concurrency API? What are it’s benefits over synchronization?

    Lock interface provide more extensive locking operations than can be obtained using synchronized methods and statements. They allow more flexible structuring, may have quite different properties, and may support multiple associated Condition objects.
    The advantages of a lock are

    • it’s possible to make them fair
    • it’s possible to make a thread responsive to interruption while waiting on a Lock object.
    • it’s possible to try to acquire the lock, but return immediately or after a timeout if the lock can’t be acquired
    • it’s possible to acquire and release locks in different scopes, and in different orders
  3. What is Executors Framework?

    In Java 5, Executor framework was introduced with the java.util.concurrent.Executor interface.
    The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies.
    Creating a lot many threads with no bounds to the maximum threshold can cause application to run out of heap memory. So, creating a ThreadPool is a better solution as a finite number of threads can be pooled and reused. Executors framework facilitate process of creating Thread pools in java. Check out this post to learn with example code to create thread pool using Executors framework.
  4. What is BlockingQueue? How can we implement Producer-Consumer problem using Blocking Queue?

    java.util.concurrent.BlockingQueue is a Queue that supports operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.
    BlockingQueue doesn’t accept null values and throw NullPointerException if you try to store null value in the queue.
    BlockingQueue implementations are thread-safe. All queuing methods are atomic in nature and use internal locks or other forms of concurrency control.
    BlockingQueue interface is part of java collections framework and it’s primarily used for implementing producer consumer problem.
  5. What is Callable and Future?

    Java 5 introduced java.util.concurrent.Callable interface in concurrency package that is similar to Runnable interface but it can return any Object and able to throw Exception.
    Callable interface use Generic to define the return type of Object. Executors class provide useful methods to execute Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object. Callable tasks return java.util.concurrent.Future object. Using Future we can find out the status of the Callable task and get the returned Object. It provides get() method that can wait for the Callable to finish and then return the result.
    Check this post for Callable Future Example.
  6. What are Concurrent Collection Classes?

    Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw ConcurrentModificationException.
    Concurrent Collection classes support full concurrency of retrievals and adjustable expected concurrency for updates.
    Major classes are ConcurrentHashMap, CopyOnWriteArrayList and CopyOnWriteArraySet, check this post to learn how to avoid ConcurrentModificationException when using iterator.
  7. What is Executors Class?

    Executors class provide utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes.
    Executors class can be used to easily create Thread Pool in java, also this is the only class supporting execution of Callable implementations.

0 comments: