Unit 4 Concurrency in Java: A closer look Flashcards

1
Q

What are the advantages and disadvantages of using virtual machines?

A

(Part 2) 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. Also 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 needing any changes, making it easier for organisations to develop applications for a wide user base. Also virtual machines facilitate the development of applications written in multiple languages and promote the creation of new languages.

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

What are the memory areas allocated to each thread? and which are allocated to the whole JVM?

A

(Part 2) Since each thread executed independently, each one needs a program counter register 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.

The objects and their code are shared by all threads and therefore a heap and method area are created on JVM start-up and allocated to the whole JVM.

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

What are some possible reasons for having two different virtual machines for clients and servers?

A

(Part 2) Servers usually have large amounts of memory and should only be rebooted occasionally; a small memory footprint and quick start-up time is therefore not essential, but execution speed is to deal with a large load of client requests.

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

Why is the semantics of the post new-Java 1.5 memory model an extension of the old one?

A

(Part 2) 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
5
Q

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

A

(Part 2) 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
6
Q

What are two problems that affect the achievement of correctness?

A

(Part 2) 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.

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

What are generic techniques that address the multithreading problems of code reordering hiding thread updates and inconsistent variables in local/shared memory?

A

(Part 2) Mutual exclusion serialises accesses and this prevents the side effects of code reordering.

To address the incicsistency problem, a memory model defines how value updates are propogated via memory barriers.

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

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

A

(Part 2) 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
9
Q

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

A

(Part 2) 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
10
Q

Through which method calls can a Java thread release control?

A

(Part 2) By calling sleep, yield, wait or join (Unit 2 sec 7)

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

What are the two ways of implementing Java threads?

A

(Part 2) The JVM specification provides two options for implementing threads: they may be green threads (managed by the JVM) or native threads (managed by the OS).

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

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

A

(Part 2) 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 better exploiting the underlying platform resources, in particular if it has multiple processors. However 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
13
Q

What are the consequences for writing platform-independent programs?

A

(Part 2) To write a program that is platform-independent, it must:

  • Not use thread priorities
  • release control via wait and sleep rather than yield whenever adequate - this avoids problems if executed on a cooperative multitasking platform
  • access shared data only under mutual exclusion - this avoids problems if executed on a pre-emptive multitasking platform
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a context where priority queues might be useful?

A

(Part 3) Priority queues are useful when implementing process schedulers (Unit 2 Sec 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
15
Q

What is a re-entrant lock?

A

(Part 3) A re-entrant lock can be reacquired by a thread that already holds it (Unit 3, Section 4).

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

What are 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

(Part 3) Some disadvantages of the built-in monitors are:

  • no seperation 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 seperate objects, and the creation of multiple conditions per lock. Also the Lock interface allows a thread to timeout on acquiring a lock or to avoid blocking if the lock is not available.

17
Q

Why should a call to wait be within a loop rather than a simple if?

A

(Part 4) 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.

18
Q

What is the simplest way of accessing an “ArrayList object via synchronised methods” ?

A

(Part 4) The simplest way is to wrap it using Collections.synchronizedList (Subsection 3.2)