Threads/Concurrency Flashcards
What are threads?
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.
Context Switching Threads
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.
Benefits of threading
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.
Concurrent Operations
Concurrent operations can be executed in parallel.
Serial Operations
Rely upon results from previous operation to do their operation.
Thread Class
A way of spawning new threads, an example being:
public class Thread extends object implements Runnable
Runnable Interface
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.
Example
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();
}
}
Java Thread States
- 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.
Order of Threads
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.
Shared Variables / Race Conditions
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
Critical Regions
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.
Semaphores
Threads must require a semaphore before it can enter a region, so other threads must wait until the semaphore is read.