Concurrency Flashcards

1
Q

What is concurrency?

A

The ability to perform multiple processes simultaneously, including the potential interaction between them

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

What part does the OS play in concurrency

A

It schedules processes

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

Give 2 reasons why we use concurrency

A

Allows programs to interact with other systems/ users

Makes use of multi-core systems

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

Multiple cores sare processes that the OS needs to run

Each process that the OS needs to run has its own memory space

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

Explain the structure of multi-threading

A

Begins by running main thread
Main thread consists of multiple child threads
When child thread starts running, main thread is free - it begins execution of the next child thread

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

How does memory allocation work with threads

A

Memory space allocated to a single process
All threads involved in that process share this memory space
Threads have their own local variables

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

What is a thread

A

Threads can be treated as lightweight processes. There are many threads within a single process.

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

Explain the structure of threads when a program is run

A

Main thread begins to run immediately
Main thread consists of many child threads
Once the first child thread begins to run, the main thread is free so it starts up the next child thread

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

How can we use sequential execution to reduce nondeterminism from multi-threaded execution

A

Map the multi-thread program to a sequential execution
Have all threads behave deterministically
Thus, entire program deterministic

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

How can multi-threading cause nondeterminism (when different outputs are produced for the same input)

A

If two child threads are at the same level, we would want them to run concurrently
OS scheduling can cause nondeterministic behaviour due to environmental conditions (e.g. memory availability, interrupts etc.)
This affects the concurrency of the two threads

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

Explain how the OS assists with the execution process of processes

A

The OS decides which cores execute which processes

Memory space is allocated per process that the OS needs to undertake

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

What is liveness

A

Ensuring code performs correctly in time

An acceptable time depends on the application

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

How do you synchronize a set of statements

A

Pass an object in as a parameter
All statements in the synchronized block will be synchronized

synchronized (this) {
    // everything in here is synchronized
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do synchronized methods work

A

One synchronized method is executed from an object
All synchronized methods for the same object block suspend execution
Until the first thread is done with the object

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

What are race conditions

A

When two threads try to access/change data at the same time

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

Name two ways to stop a thread

A

Setting a flat => when the loop exits, thread will stop
Interrupting => thread stops with isInterrupted() called; if thread sleeping/waiting, InterruptedException is thrown and returns from run

17
Q

What is a disadvantage of extending from the Thread class when creating threads

A

You can only extend from one class - multiple inheritance not allowed
Waste of inheritance if we don’t make use of all the resources provided by the inheritance

18
Q

What do each of “new” keywords do in the following code

new Thread (new HelloRunnable()).start()

A

1) Creates a new instance of the thread

2) Creates an instance of a class (anonymous inner)

19
Q

Name two ways threads can be created in Java

A

Implementing Runnable

Extending Thread

20
Q

How do you synchronize a set of statements

A

Pass an object in as a parameter
All statements in the synchronized block will be synchronized

synchronized (this) {
    // everything in here is synchronized
}
21
Q

What are atomic actions? How do you declare them?

A

Actions that are self-contained; they cannot be stopped in the middle
They’re declare volatile
Reads and writes are atomic for most primitive variables