Threads Flashcards

(11 cards)

1
Q

Concurrency

A

Managing multiple tasks at the same time, but not necessarily doing them literally at the same instant

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

Parallelism

A

Doing multiple things at the exact same time, using multiple CPU cores

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

Can an application be concurrent but not parallel?

A

Yes e.g. multitasking on a single-core CPU

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

What is a thread?

A

A mini-program running inside a main program. Programs can have multiple threads doing tasks at the same time

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

Why use multithreading?

A
  • Responsiveness: Avoid blocking
  • Resource Sharing: Threads share memory by default
  • Economy: Faster to create/switch than processes
  • Scalability: Utilises multiple cores effectively
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the states of a Java thread?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Amdahl’s Law?

A

Speedup <= 1/ S+[(1-S)/N], S=serial portion, N=cores. Limits speedup due to serial code

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

Name multicore programming challenges

A

Load balancing, Data dependencies/synchronisation, debugging parallel tasks

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

Why use multithreading in servers?

A
  • Single-threaded: Poor responsiveness (one client at a time)
  • Process-per-request: Heavy overhead
  • Multithreaded: Efficiently handle multiple requests (one thread per request)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creating Threads in Java, Method 1: Extend Thread class

A

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();
}
}

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

Creating Threads in Java, Method 2: Implement Runnable

A

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();
}
}

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