Unit 4 Flashcards
SAQ 1
Summarise the advantages and disadvantages of using virtual machines.
The main disadvantage of using virtual machines is the slower execution speed of the program, compared with compiling it directly to machine code that is interpreted by the CPU. Moreover, virtual machines are also subject to backward compatibility problems and to the limitations of the various platforms.
The advantages are that the generated virtual machine code is smaller than the corresponding machine code and can be executed on various platforms without requiring any changes, making it easier for organisations to develop applications for a wide user base. Moreover, virtual machines facilitate the development of applications written in multiple languages and promote the creation of new languages.
Exercise 1
Which of the memory areas are allocated to each thread, and which are allocated to the whole JVM?
Since each thread executes independently, each one needs a program counter and a stack of activation frames, in order to keep track of which methods have been called and which instruction of the current method is being executed. However, the objects and their code are shared by all threads, and therefore the heap and method area are created on JVM start-up and allocated to the whole JVM.
SAQ 2
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
SAQ 4
Through which method calls can a Java thread release control?
By calling sleep, yield, wait or join
SAQ 6
Can you recall 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
SAQ 8
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.
SAQ 9
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.
SAQ 10
What is the simplest way of accessing ‘the ArrayList object via synchronised methods’, as suggested above?
The simplest way is to wrap it using Collections.synchronizedList
SAQ 11
What are the thread-safety levels of LinkedList and CopyOnWriteArrayList mentioned in Subsection 3.2?
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.