Units 3 and 4 Flashcards

1
Q

What is the Java Memory model?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a memory barrier and how does it work?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are two ways of implementing Java threads?

A

Green threads - managed by the JVM

Native threads - managed by the OS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The consequences of threading models in writing platform independant systems

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the built in Java concurrency models?

A

synchronized, wait, notifyAll and volatile variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the limitations of Java’s built in concurrency models?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Java 1.5 introduced java.util.concurrent package. What are the advantages of this?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the advantages of the BlockingQueue interface?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages of CopyOnWriteArrayList, what exception does it avoid and where is it useful?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When would using the java.util Queue interface be recommended?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When would a ConcurrentModificationException be thrown?

A

When one thread modifies a collection while another is iterating over it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the aim of java.util.concurrent.atomic?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does scalability under contention mean in terms of atomic variables?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe how locks work in Java Concurrency utilities.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Reentrant Lock?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the Condition interface improve concurrency issues?

A
It allows multiple explicit condition variables to be defined by the programmer and called on the Lock object 
e.g. provide Condition notFull = lock.newCondition();
The methods of Condition interface are as follows
void await() //
boolean await(timed)
void Signal()
void signalAll()
Advice to put this in try-finally so lock is released in all cases.
17
Q

List some shortcomings of Java’s built in monitors and how they are addressed by the interfaces and classes of the java.util.concurrent.locks package.

A

Disadvantages are no separation of data, lock and condition variables, one single condition variable per lock, an attempt to acquire a lock cannot be cancelled.
The Lock and Condition interfaces allow the creation of explicit Locks that are separate objects and the creation of multiple conditions per lock. Moreover the lock interface allows a thread to timeout on acquiring a Lock or to avoid blocking if the lock is not available.

18
Q

What are the advantages of the java.util.concurrent.Semaphore class?

A
This allows high level abstraction to facilitate coordination among several threads.
constructor Semaphore (permits)
methods
void acquire( int permits)
void release(int permits)
boolean tryAcquire(int permits)
boolean tryAcquire (int permits, timed)
19
Q

How does Countdown Latch work?

A

part of concurrency utilities.
Implements a thread exectuion barrier, if the counter is positive the barrier is down andany thread arriving at the latch has to wait. if the counter is zero then any waiting threads can resume execution. The latch has only await and countdown as methods. It allows one group of threads to wait until another group of threads has perfomed some operation.

20
Q

What are the rules on thread safety?

A
Synchronise access to shared mutable data
Avoid unecessary synchronisation
Always call wait inside a loop
do not depend on the thread scheduler
Document thread safety.
21
Q

What are the levels of thread safety?

A

Immutable
Immutable objects are guaranteed to be thread-safe, and never require additional synchronization. e.g String,
Thread-safe
May be accessed by multiple threads
Conditionally thread-safe
certain sequences of operations may require external synchronization.
If you are documenting a conditionally thread-safe class, you should document which sequences of operations must be protected from concurrent access.
Thread-compatible
Thread-compatible classes are not thread-safe, but can be used safely in concurrent environments by using synchronization appropriately.
Thread-hostile
Thread-hostile classes are those that cannot be rendered safe to use concurrently,