Units 3 and 4 Flashcards
What is the Java Memory model?
A set of conditions that will guarantee that a value written by the currently running thread will be visible to other threads and vice versa.
What is a memory barrier and how does it work?
A memory barrier ensures consistency between variables and main memory. It uses cache flush (updating the cache values to main memory on acquisition of a lock) and cache invalidation (updating values from main memory to cache on release of a lock)
What are two ways of implementing Java threads?
Green threads - managed by the JVM
Native threads - managed by the OS
The consequences of threading models in writing platform independant systems
For a program to be truly independant it should
- not use thread priorities
- release control via wait and sleep rather than yield to avoid problems if executed on a cooperative multitasking platform
- access shared data only under mutual exclusion so as to avoid problems if executed on a pre-emptive multitasking platform.
What are the built in Java concurrency models?
synchronized, wait, notifyAll and volatile variables
What are the limitations of Java’s built in concurrency models?
Java monitors have a single anonymous condition variable.
A thread cannot have timed blocking
Poor support for priorites - dependant on the underlying system threading model
Java 1.5 introduced java.util.concurrent package. What are the advantages of this?
- collections that perform more flexibly under concurrent access
- variables that can be compared and set atomically on many platforms
- lock objects with multiple condition variables
- a general framework for scheduling asynchronous tasks in an efficient way
- high level abstractions including semaphores in order to coordinate threads more easily
What are the advantages of the BlockingQueue interface?
java.util.concurrent.BlockingQueue adds the following methods
public void put(Object o)
public Object take ()
both force a thread to wait if the operation is not possible
What are the advantages of CopyOnWriteArrayList, what exception does it avoid and where is it useful?
part of java.util.concurrent package, a synchronised version of ArrayList with a weakly consistent iterator.
As it uses a copy it can not raise an exception such as ConcurrentModificationException. Useful where reads outnumber writes.
When would using the java.util Queue interface be recommended?
When you don’t need the full flexibility of List operations (retrieving, adding and removing items from a particular position).
Queues are often used in consumer/producer settings and may be bounded or unbounded.
When would a ConcurrentModificationException be thrown?
When one thread modifies a collection while another is iterating over it.
What is the aim of java.util.concurrent.atomic?
It includes classes Atomic Integer and Atomic Boolean.
Atomic variables make it possible to write thread safe counters without using synchronised code. Atomic variables always read and write from shared memory.
What does scalability under contention mean in terms of atomic variables?
Scalability under contention refers to the capacity to maintain a high throughput when multiple threads are trying to access the same resource. As atomic variables use the hardware low level TAS instructions where possible there is far less overhead on the JVM in acquiring locks and blocking threads. Therefore the code will execute faster providing better scalability under contention.
Describe how locks work in Java Concurrency utilities.
They have to be explicitly acquired and released by calling lock and unlock.
The Lock includes 2 boolean methods to try and acquire a lock without blocking if the lock is not available:
- tryLock() returns false if Lock is unavailabe, or else it acquires it and returns true
- tryLock(long time , TimeUnit unit) as above but with time limit returning true or false
What is a Reentrant Lock?
Its a concurrency utility implementation of the Lock interface, which provides a lock that may be reaquired by a thread that already holds it. The class ReEntrant Lock is implemented in terms of atomic variables, so as long as the underlying hardware has TAS instructions it performs better and provides better scalability under contention.