Concurrency Flashcards

1
Q

What are the conceptual from of tasks?

A

A process is started when the Java program is first ran, causing the OS to allocate it resources. It starts with one executing three

One or more threads exist within a process and can execute to complete a task.

A task is a set of program instructions as defined by the written code.

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

What are the measures of success for a concurrent application?

A

Efficiency - how little computing resources can be used

Responsiveness - how soon can events be responded to

Task separation - allows for interleaving i.e. stops one task blocking the others

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

What is the difference between threads and processes?

A

Both get their own method call stack and program counter (keep track of which line of code). However, threads share the memory heap wiht threads from the same process.

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

How can threads be paused to allow execution of other threads?

A

Either voluntarily in “cooperative scheduling” in which they yield when able to.

Otherwise the SO interrupts them - “preemptive time slicing”

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

How do Java threads relate to OS supported threads?

A

OS-supported threads run each thread on the same CPU / core. Threads from the same process may run on different CPUs.

Java threads run as OS-supported threads, but the JVM typically switches them between different OS-supported threads over time.

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

What is the basic lifecycle of a thread?

A

They start as “new” when created by become “runnable”.

When runnable, the OS manages the transition between “ready” and “running”

Other states it may be in include waiting, timed waiting (wait but with timeout), blocked (waiting to acquire a lock etc.) and terminated (it is done)

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

How is the transition from ready to running managed?

A

This is all hidden from the JVM - both are simply encompassed by Runnable.

The OS determines when to “dispatch” the thread and hence move it on to the processor. This is called “thread scheduling”

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

How are threads created?

A

By defining a new class that extends the Thread class. Specifically the run() method must be overridden.

An instance of that class is then created and then executing by calling .start() on it.

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

What parameter must thread constructors have?

A

The name of the thread. Optionally it can also have a custom runnable.

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

How can threads be created using the Runnable interface?

A

Define a new class that implements the Runnable interface.

Implement a run() with the task to be executed.

Instantiate a new Thread with the custom runnable as the constructor, as well as the thread name. Execute this thread.

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

How can the current thread be accessed?

A

With Thread.currentThread().

For example, the name of the current thread can be retrieved with Thread.currentThread().getName()

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

When is it best to implement Runnable versus extend Thread?

A

When it makes sense to extend from a different class (multiple inheritance is not allowed)

The Runnable instance can be used over multiple threads, helping reduce memory consumption.

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

What are the two basic types of thread?

A

Daemon threads provide support to non-daemon threads. These do not help keep the application alive.

Non-daemon threads are the key threads of the application such as the main thread (for console applications) or the event dispatch thread (GUI).

If all non-daemon threads have terminated then the daemon threads, and hence entire application, is terminated.

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

When a thread is created, what type is it by default?

A

A user thread (non-daemon)

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

What are daemon and non-daemon threads also called?

A

Daemon - background, service

Non-daemon - user threads

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

How can threads be made daemon or not?

A

With myThread.setDaemon(true/false). This must be done BEFORE the thread is started.

To determine if a thread is daemon or not, use .isDaemon

17
Q

What is an alternative way of executing Runnable objects?

A

This object can be passed to an ExecutorService which manages a pool of reusable threads behind the scenes.

18
Q

How are executor services used?

A

They are created using the Executors.newCachedThreadPool() static method.

Runnable can be executed using service.execute(runnable)

19
Q

How can an executor with a specific number of threads be created?

A

Use Executors.newFixedThreadPool(int nThreads)

20
Q

What methods do executors provide to manage termination?

A

shutdown() - initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks are

shutdownNow() - attempts to stop all executing tasks

awaitTermination() - blocks the invoking thread until all tasks have been completed / timed out

21
Q

What are race conditions?

A

When multiple threads are accessing mutable shared data at the same time

22
Q

What are atomic operations?

A

Those that operate on share memory as a single step from the perspective of other threads.

Therefore, intermediate values cannot be messed with

23
Q

What are the methods of AtmoicInteger?

A

get()

getAndIncrement()

getAndAdd(int delta)

24
Q

How can methods be made thread safe?

A

With the “synchronised” keyword i.e. public synchronised void doSomething() { }

This allows only one thread at a time to execute the method. Any other thread will be blocked.

25
Q

How does the synchronised keyword work behind the scenes?

A

Every object in Java has Monitor. This ensures that every MonitorLock is held by a maximum of one thread at a time

26
Q

How are synchronised blocks used?

A

They are used inline i.e. synchronised(object) { … }.

Note that they must declare which object is being synchronised. For example, they can synchronise on single instance variables

27
Q

What is a producer/consumer relationship?

A

A common pattern in which one thread (the producer) generates data and stores it in a shared object. They consumer thread reads the data from the other end of the shared object.

The data structure is called a buffer

28
Q

How do waiting threads work?

A

Threads can be placed into a waiting state by calling the .wait() method of the object class. It will continue waiting until it is notified buy another thread that it can continue.

Threads can notify other threads by calling notify() or notifyAll()

29
Q

How does Java provide thread-safe collections?

A

With BlockingQueue() and ArrayBlockingQueue(int capacity)

Alos synchronisedList

30
Q

What does BlockingQueue do?

A

It is a queue that supports operations that wait for the queue to be non-empty when retrieving and wait for space when storing an element.

31
Q

What does ArrayBlockingQueue do?

A

It is a bounded blocking queue backed by an array. Its methods include:

Add() - insert or throw error if no space
Put() - insert but wait if no space
Remove() - return head or throw error if empty
Poll() - return head or wait until timeout

32
Q

What is Deadlock?

A

When thread A is waiting for thread B but B is waiting for A

33
Q

What is starvation?

A

Threads with low priority might never get ran if higher priority threads keep getting ran instead

34
Q

In the context of GUI, what are windows?

A

Top-level containers i.e. JFrame

35
Q

In the context of GUI, what are components?

A

GUI widgets that can be placed inside containers

36
Q

In the context of GUI, what are containers?

A

Logical grouping of components

Notably they are responsible for laying out these components using LayoutManagers