Flashcards in M362 - Unit 4 Deck (19):
Summarise the advantages and disadvantages of using virtual machines.
Disadvantages: slower execution speed of the program (compared with compiling it directly to machine code that is interpreted by the CPU), subject to backward compatibility problems and to the limitations of the various platforms.
Advantages: generated VM code is smaller than the corresponding machine code, can be executed on various platforms without requiring any changes, facilitate the development of applications written in multiple languages and promote the creation of new languages.
Why is the new semantics of volatile variables an extension of the old one?
Because a memory barrier on each volatile variable access still forces the variable to be read from or written to shared memory, as required by the old memory model.
Indicate one of the central concerns for correctness of multithreaded applications.
For a multithreaded application to behave correctly, changes made to shared variables by one thread must be visible to all other threads reading those variables.
Indicate two problems that affect the achievement of correctness.
Updates in one thread might not be visible to other threads due to code reordering or because the values’ copies held in local and shared memories are inconsistent.
Indicate the generic techniques that address the problems of code reordering and inconsistency.
Mutual exclusion serialises accesses and this prevents the side effects of code reordering. To address the inconsistency problem, a memory model defines how value updates are propagated via memory barriers.
Name the concrete Java mechanisms that implement the techniques of code reordering and inconsistency.
Mutual exclusion is imposed by synchronised code. Memory barriers that make the values held locally by the thread consistent with those held in shared memory are created by accessing volatile variables or by synchronising code.
How can the mechanisms that implement the techniques of code reordering and inconsistency be used.
Correct behaviour can be guaranteed only if threads synchronise on the same monitor or access the same volatile variable. Code is not reordered across memory barriers, which makes it possible to use volatile variables to flag changes to shared variables.
Through which method calls can a Java thread release control?
By calling sleep, yield, wait or join
Name the two ways of implementing Java threads (in the JVM specifically)
Among the decisions left open by the JVM specification is how to implement Java threads: they may be green threads, i.e. managed by the JVM, or native threads, i.e. managed by the operating system.
Name the advantages and disadvantages of the two ways of implementing Java threads.
Using green threads may be the best option if the underlying platform does not have threads, but the disadvantage is that the JVM implementation becomes more complex. Using native threads has the advantage of exploiting better the underlying platform resources, in particular if it has multiple processors. On the other hand, native threads make the behaviour of the Java program dependent on the way the operating system schedules and prioritises threads.
Name the consequences for writing platform-independent programs.
This means that for a program to be truly platform independent it should:
- not use thread priorities;
- release control–via wait and sleep rather than yield – whenever adequate, 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.
Can you recall, from earlier in the course, a context where priority queues might be useful?
Priority queues are useful when implementing process schedulers (Unit 2, Section 5). Each element in the queue consists of a pair of numbers – the unique process ID and the process priority. The next process to be scheduled is the one at the front of the priority queue.
What is a re-entrant lock?
A re-entrant lock can be reacquired by a thread that already holds it
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.
Some disadvantages of the built-in monitors 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.
Why should the call to wait not be within a simple if?
When a thread T resumes execution after a call to o.wait(), this only means two things: T was notified by some other thread, and T has reacquired the lock on object o. In particular, resumption of execution does not mean that the condition has become true. For example, T might have been woken together with other threads by a notifyAll, and one of those threads may have acquired the lock on o first and invalidated the condition again.
What is the simplest way of accessing an ArrayList object via synchronised methods
The simplest way is to wrap it using Collections.synchronizedList
What are the thread-safety levels of LinkedList and CopyOnWriteArrayList
LinkedList is thread-compatible, like ArrayList, while CopyOnWriteArrayList is conditionally thread-safe. The latter is not thread-safe because different threads may see the data structure in different states due to the weakly consistent iterators.
What is scalability under contention, and how do atomic variables help to achieve it?
Scalability under contention is the capacity to maintain a high throughput when multiple threads are trying to use the same resource. Using an atomic variable means there are no locks and therefore threads don't get blocked, and the JVM spends less time scheduling threads. Atomic variables can therefore make the code simpler and also make it run faster.
Name the 5 components of the Java Virtual Machine.
Bytecode verifier (checks whether the bytecode about to be executed has not been tampered with).
Class loader (loads classes at runtime into the JVM's memory).
Bytecode interpreter (executes the bytecode).
Garbage collector (reclaims the memory occupied by unused objects from the heap).
JIT compiler (translates VM code into machine code the first time the program is executed).