UNIT 4 Flashcards

1
Q

4.1 Summarise the advantages and disadvantages of using virtual machines.

A

Advantages:

  • the generated virtual machine code is smaller than the corresponding machine code
  • it can be executed on various platforms without any changes, easier to develop apps for a wide user base.
  • VMs facilitate the development of apps written in multiple languages, and promote the creation of new languages.

Disadvantages:

  • slower execution speed of the program, compared with compiling it directly to machine code that is interpreted by the CPU.
  • are subject to backward compatibility problems
  • are subject to the limitations of the various platforms.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

4.2 Why is the new semantics of volatile variables an extension of the old one?

A

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.

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

4.3a What is one of the central concerns for correctness of multithreaded applications?

A

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.

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

4.3b What are two problems that affect the achievement of correctness?

A
  • Updates in one thread might not be visible to other threads due to code reordering
  • The copies of values held in local and shared memories might be inconsistent.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

4.3c What are the generic techniques that address the problems of code reordering and inconsistency?

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

4.3d What are the concrete Java mechanisms that implement mutual exclusion and memory barriers?

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

4.3e How can the mechanisms of synchronised code and volatile variables be used?

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

4.4 Through which method calls can a Java thread release control?

A

By calling sleep, yield, wait or join

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

4.5a What are the two ways of implementing Java threads in a JVM?

A

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

4.5b What are the advantages and disadvantages of green and native threads?

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

4.5c What are the consequences of JVM and OS thread variations for writing platform-independent programs.

A

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

4.6 Can you recall, from earlier in the course, a context where priority queues might be useful?

A

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.

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

4.7 What is a re-entrant lock?

A

Are-entrant lock can be reacquired bya thread that already holds it.

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

4.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.

A

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.

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

4.9 Why should the call to wait() be in a while loop and not be within a simple if?

A

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.

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

4.10 What is the simplest way of accessing ‘the ArrayList object via synchronised methods’, as suggested above?

A

The simplest way is to wrap it using Collections.synchronizedList

17
Q

4.11 What are the thread-safety levels of LinkedList and CopyOnWriteArrayList mentioned in Subsection 3.2?

A

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.

18
Q

Ex1 What are the 4 JVM memory areas (not hardware mem)?

Which of the above memory areas are allocated to each thread, and which are allocated to the whole JVM?

A
  • a program counter register to hold the address of the instruction being executed;
  • a stack of activation frames;
  • a heap to hold the objects;
  • a method area to hold the code of the methods.

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.