Saturday, 25 May 2013

Java Concurrency Interview Questions


What is concurrency? Concurrency issues.
Concurrency is the property of computer systems which allows multiple tasks to be executed in parallel. Tasks can be executed by preemptively time-sharing on a single chip, on multiple cores on the same chip, on different chips or by dividing the task in multiple smaller non-blocking events and queueing them for execution. Concurrency introduces two main issues: consistency and deadlocks.

How is concurrency implemented in Java?
In Java, concurrency is based on processes and threads. A Java program runs in it’s own process that can start multiple threads.

What is synchronization and why is it important?
Concurrent tasks may be executed to access shared resources. With synchronization we solve the consistency and interference problems.

Describe synchronization in respect to multithreading.
Threads communicate by sharing fields and the objects the reference fields refer to. While efficient, this causes thread interference and memory consistency errors that are solved through synchronization.

What does memory consistency mean?
All threads should have the same image of the data at any time.

What is a deadlock and how does it manifest?
A deadlock occurs when two threads are waiting each other and can’t proceed.

Describe liveness in respect to concurrency.
Liveness is the ability of an application to execute in a timely manner. Liveness problems are deadlocks, starvation and livelock. Deadlock occurs when threads wait for each other and can proceed. Starvation occurs when a thread cannot gain access to synchronized resources because of greedy threads. Livelock occurs when threads are too busy responding to each other and essentially can’t proceed, even if they are not blocked.

What are atomic operations?
An atomic operation is effectively happening at once, it cannot stop in the middle. Read and writes for reference types and all primitives except longand double are atomic. Read and writes on all variables declared volatile are atomic.

Describe immutability. Give examples.
Immutability refers to objects that cannot change their state after they have been constructed. Strings and wrapper classes are immutable in Java.

Explain different ways of using a thread.
You can inherit from Thread class or implement the Runnable interface.

When a thread is created and started, what is its initial state?
Ready state.

What are synchronized methods and synchronized statements?
Synchronized code can only be executed by one thread at a time. When a thread executes a synchronized method it acquires an intrinsic lock on the method’s object or Class object (in case the method is static). When a thread executes a synchronized statement, it acquired an intrinsic lock on a specified object.

What is daemon thread and which method is used to create the daemon thread?
Any Java thread can be a daemon thread. Daemon threads live to service other user threads. When the only threads running are daemon threads, the application exists. Use setDaemon(true) to make a thread daemon.

What method must be implemented by all Threads?

Why would you use a synchronized block instead of synchronized method?
Synchronized methods sets an intrinsic lock on the method’s object, effectively making it possible to execute only one method at a time. Use multiple blocks with multiple locks when they don’t generate interference. Also, only with blocks you can set an intrinsic lock on an external object.

What’s the difference between the methods sleep() and wait()?
sleep temporary ceases execution of the thread for a specified time. waitceases execution until notify is called on the object or notifyAll is called by another thread.

How can i tell what state a thread is in?

What methods is Java providing for thread communications?
notify, notifyAll, wait
What is the difference between notify() and notifyAll() methods?
Notify wakes an arbitrary thread that is waiting on the object’s monitor. NotifyAll wakes all threads that wait on the object’s monitor. Both methods should be called by the owner of the object’s monitor. The awakened thread will proceed once the lock is relinquished on the object and has no privilege or disadvantage on gaining a lock on the object.

What is Runnable interface ? Are there any other ways to make a multi-threaded Java application?
You can implement the Runnable class and delegate running it to a class that extends the Thread class. You can also extend the Thread class and override the run() method.

Why do threads block on I/O?
In BLOCKED state, the thread waits for a monitor lock. This means that other threads can gain the lock while waiting for I/O.

When a thread blocks on I/O, what state does it enter?

What’s new with the stop(), suspend() and resume() methods inJDK 1.2?

What is the difference between yielding and sleeping?
While sleeping, the thread does not lose ownership of the monitor. Yielding loses ownership.

What invokes a thread’s run() method?
It’s called by the JVM after the thread has been started.

How can you restart a thread?
It’s not legal to start a thread more than once.

What method is invoked to cause an object to begin executing as a separate thread?

Give examples of thread-safe collections. What are their drawbacks?
Classes from java.util.concurrent package: BlockingQueue, ConcurrentMap, ConcurrentHashMap, ConcurrentNavigableMap, ConcurentSkipListMap. Other common synchronized collections: HashTable, Vector, as opposed to HashMap and ArrayList). Note also StringBuilder that is thread safe as opposed to StringBuffer.

What is thread safe? How do you make sure an API is thread safe?
Thread safe assumes that your API can be used in a multi-threaded application without adding an extra synchronization layer. Use immutable objects, synchronization and/or use atomic operations.

What is the difference between preemptive scheduling and time slicing?
Preemptive scheduling assumes that the running thread must get out of running state before executing another thread (e.g. blocking I/O, preempted by a higher priority thread, explicitly call a scheduling methods such as wait, sleep or yield). With time slicing, each thread is allowed to execute for a limited amount of time.