Java Multithreading Interview Set 4
Categories: Java 8(JDK1.8)
What are the main components of concurrency API?
Concurrency API can be developed using the class and interfaces of java.util.Concurrent package. There are the following classes and interfaces in java.util.Concurrent package.
a) Executor
b) FarkJoinPool
c) ExecutorService
d) ScheduledExecutorService
e) Future
f) TimeUnit(Enum)
g) CountDownLatch
h) CyclicBarrier
i) Semaphore
j) ThreadFactory
What is the Executor interface in Concurrency API in Java?
The Executor Interface provided by the package java.util.concurrent is the simple interface used to execute the new task. The execute() method of Executor interface is used to execute some given command. The syntax of the execute() method is given below.
What is BlockingQueue?
The java.util.concurrent.BlockingQueue is the subinterface of Queue that supports the operations such as waiting for the space availability before inserting a new value or waiting for the queue to become non-empty before retrieving an element from it. Consider the following example.
What is the difference between Java Callable interface and Runnable interface?
The Callable interface and Runnable interface both are used by the classes which wanted to execute with multiple threads. However, there are two main differences between the both :
A Callable <V> interface can return a result, whereas the Runnable interface cannot return any result.
A Callable <V> interface can throw a checked exception, whereas the Runnable interface cannot throw checked exception.
A Callable <V> interface cannot be used before the Java 5 whereas the Runnable interface can be used.
What is the Atomic action in Concurrency in Java?
a) The Atomic action is the operation which can be performed in a single unit of a task without any interference of the other operations.
b) The Atomic action cannot be stopped in between the task. Once started it fill stop after the completion of the task only.
c) An increment operation such as a++ does not allow an atomic action.
d) All reads and writes operation for the primitive variable (except long and double) are the atomic operation.
e) All reads and writes operation for the volatile variable (including long and double) are the atomic operation.
f) The Atomic methods are available in java.util.Concurrent package.
What is lock interface in Concurrency API in Java?
The java.util.concurrent.locks.Lock interface is used as the synchronization mechanism. It works similar to the synchronized block. There are a few differences between the lock and synchronized block that are given below.
a) Lock interface provides the guarantee of sequence in which the waiting thread will be given the access, whereas the synchronized block doesn't guarantee it.
b) Lock interface provides the option of timeout if the lock is not granted whereas the synchronized block doesn't provide that.
c) The methods of Lock interface, i.e., Lock() and Unlock() can be called in different methods whereas single synchronized block must be fully contained in a single method.
What is the difference between Synchronous programming and Asynchronous programming regarding a thread?
Synchronous programming: In Synchronous programming model, a thread is assigned to complete a task and hence thread started working on it, and it is only available for other tasks once it will end the assigned task.
Asynchronous Programming: In Asynchronous programming, one job can be completed by multiple threads and hence it provides maximum usability of the various threads.
What do you understand by Callable and Future in Java?
Java Callable interface: In Java5 callable interface was provided by the package java.util.concurrent. It is similar to the Runnable interface but it can return a result, and it can throw an Exception. It also provides a run() method for execution of a thread. Java Callable can return any object as it uses Generic.
Syntax:
public interface Callable<V>
Java Future interface: Java Future interface gives the result of a concurrent process. The Callable interface returns the object of java.util.concurrent.Future.
Java Future provides following methods for implementation.
cancel(boolean mayInterruptIfRunning): It is used to cancel the execution of the assigned task.
get(): It waits for the time if execution not completed and then retrieved the result.
isCancelled(): It returns the Boolean value as it returns true if the task was canceled before the completion.
isDone(): It returns true if the job is completed successfully else returns false.
What is the difference between ScheduledExecutorService and ExecutorService interface?
ExecutorServcie and ScheduledExecutorService both are the interfaces of java.util.Concurrent package but scheduledExecutorService provides some additional methods to execute the Runnable and Callable tasks with the delay or every fixed time period.