Threads/Concurrency Flashcards

1
Q

What are threads?

A

A thread is a mini-process within another process that has its own instruction pointer and stack space. This mini-process is ran independently, but shares its code and data with other threads which originated from the original process.

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

Context Switching Threads

A

With threads, the kernel does not handle the context switch of individual threads; they are handled at a higher level within the main process by the thread library. For example, Java threads are handled by the Java Virtual Machine, in which these threads are called user-level threads since they are user level, not kernel level.

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

Benefits of threading

A

Ease of Programming -> can create threads instead of doing everything in one piece of linear code.
- Responsiveness -> multithread applications can continue to run even if one thread is block.
- Resource Sharing -> threads can share memory and resources to the main process.
- Economy -> threads are easier to create and switch between because they are allocated and managed within the main process.
- Parallel Execution -> each process can be physically running at the same time if the CPU has multiple cores.

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

Concurrent Operations

A

Concurrent operations can be executed in parallel.

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

Serial Operations

A

Rely upon results from previous operation to do their operation.

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

Thread Class

A

A way of spawning new threads, an example being:
public class Thread extends object implements Runnable

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

Runnable Interface

A

Defines a run() method that the user must implement, with the code not executing until the main program requests it.
We can then create an instance of the thread and call its start() method.

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

Example

A

class MyWorker extends Thread {
public void run() {
System.out.println(“I am the worker thread”);
}
}

public class MyMain {
public static void main(String[] args) {
MyWorker runner = new MyWorker();
runner.start();
}
}

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

Java Thread States

A
  • Runnable -> when start() is called.
  • Block -> when sleep() is caled, or if it is waiting for IO, or waiitng for another thread to finish.
  • Dead -> has terminated and is waiting for JVM to clean up its memory via garbage collection.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Order of Threads

A

There is no guarantee that the threads we create will execute in the order we give them. For example, when we have two start()’s in our program, they could operate at any given time, not one after the other.

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

Shared Variables / Race Conditions

A

Lets say we had two threads in our program, which both increment a value. In order to increment it, we take the values from the variable and put it into eax. We then increment eax and assign the value to the variable. However, if a thread starts executing their program before the first thread has finished, we are left with only one incrementation (specifically talking about assemly language code). The sequence would be:
MOV (eax = 0) -> T1
INC (eax = 1) -> T1
MOV (eax = 0) -> T2
INC (eax = 1) -> T2
MOV (num = 1) -> T2
MOV (num = 1) -> T2

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

Critical Regions

A

A piece of code is in a critical region when a process or thread is accessing a shared resource. Its critical because if another thread accesses it whilst the first thread is executing its own process, we then get a potential loss of data.

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

Semaphores

A

Threads must require a semaphore before it can enter a region, so other threads must wait until the semaphore is read.

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