Java concurrency Flashcards
How can you create a thread?
You can create a thread by invoking the start() method on an object of the Thread class or you can implement the Runnable interface
What happens if an exception gets thrown inside a synchronized block?
The lock will be released
Why cant you declare constructors synchronized?
The jvm ensures that only one thread can invoke a constructor call(for a specific constructor) at a given point in time
When does a deadlock arises?
When locking threads result in a situation where they cannot proceed and thus wait indefinitely for others to terminate
What is the purpose of CyclicBarrier?
Wait for a number of threads wait until they are synchronized at one point, and then another thread can run
What are the three main differences between ArrayList and CopyOnWriteArrayList?
- ArrayList is not thread-safe but CopyOnWirteArrayList is thread safe
- Methods in ArrayList such as remove(), add() and set() methods can throw java.util.ConcurrentModificationException is another thread modifies the ArrayList when one thread is acessing it. However, it is safe to perform these operations from multiple threads in CopyOnWriteArrayList
- You can get an iterator by calling the Iterator() method on a List object. If you call remove() method when the underlying container is modified, you may get a ConcurrentModificationException. However, you cannot call the remove() method on an Iterator of a CopyOnWriteList: It always throws the UnsupportedOperationException
What is Executor?
is an interface that declared only one method: void execute(Runnable)
What is Callable?
Callable is an interface that declares only one method: call(). Its full signature is V call() throws Exception. It represents a task that needs to be completed by a thread. Once the task completes, it returns a value. If the call() method cannot execute or fails, it throws an Exception
What is a thread pool?
Is a collection of threads that can execute tasks.
How can you create a Thread pool?
You can create a thread pool using the Executors utility class. This class provides methods to get instances of threads pools, thread factories, and so on
What is ExecutorService?
the ExecutorService interface extends the Executor interfaces and provides services such as termination of threads and production of Future objects. Some tasks may take considerable execution time to complete, so when you submit a task to the executor service, you get a Future object
What is Future class?
Future represents objects that contain a value that is returned by a thread in the future(ie. returns the value once the hread terminates in the “future”). You can use the isDone() method in the Future class to check if the task is complete and then use the get() method to fetch the task result. If you call the get() method directly while the task is not complete, the method blocks until it completes and returns the value once available
What is the use of the Fork/Join framework?
It helps simplify writing parallelized code. The framework is an implementation of the ExecutorService interface and provides an easy to use concurrent platform in order to exploit multiple processors. Drive and conquer problems. This approach is suitable for tasks that can be divided recursively and computed on a smaller scalled; the computed results are then combined.
What is forking?
dividing smaller tasks
What is joining?
merging the results from the smaller tasks