1/10
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
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?
Responsiveness: Avoid blocking
Resource Sharing: Threads share memory by default
Economy: Faster to create/switch than processes
Scalability: Utilises multiple cores effectively
What are the states of a Java thread?
NEW: Created but not started
RUNNABLE: Ready or running (scheduler decides)
BLOCKED: Waiting for a monitor lock
WAITING: Indefinite wait
TIMED_WAITING: Waits with timeout
TERMINATED: Completed execution
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?
Single-threaded: Poor responsiveness (one client at a time)
Process-per-request: Heavy overhead
Multithreaded: Efficiently handle multiple requests (one thread per request)
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(); } }