Concurrency
Managing multiple tasks at the same time, but not necessarily doing them literally at the same instant
Parallelism
Doing multiple things at the exact same time, using multiple CPU cores
Can an application be concurrent but not parallel?
Yes e.g. multitasking on a single-core CPU
What is a thread?
A mini-program running inside a main program. Programs can have multiple threads doing tasks at the same time
Why use multithreading?
What are the states of a Java thread?
What is Amdahl’s Law?
Speedup <= 1/ S+[(1-S)/N], S=serial portion, N=cores. Limits speedup due to serial code
Name multicore programming challenges
Load balancing, Data dependencies/synchronisation, debugging parallel tasks
Why use multithreading in servers?
Creating Threads in Java, Method 1: Extend Thread class
class MyTask extends Thread {
public void run() {
System.out.println(“Task is running!”);
}
}
public class Main {
public static void main(String[] args) {
MyTask t = new MyTask();
t.start();
}
}
Creating Threads in Java, Method 2: Implement Runnable
class MyTask implements Runnable {
public void run() {
System.out.println(“Task is running”);
}
}
public class Main {
public static void main(String[] args) {
Threads t = new Thread(new MyTask());
t.start();
}
}