Concurrency: Foundation & Executor Framework Flashcards

1
Q

What are the 2 ways of implementing a thread?

A
  1. Extend the Thread class and overwrite the run() method

2. Implement the Runnable interface in a separate or anonymous class

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

Which method should we use to execute the run() method of a thread?

A

the start() method

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

What’s the purpose of the join() method?

A

With join() a thread can call another thread. Itself will be blocked until the other thread is terminated.

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

In what states is the thread alive?

A

RUNNABLE (Ready, Running)

BLOCKED, WAITING, TIMED_WAITING (“Suspended”)

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

What are the 6 different states of a thread?

A
NEW
RUNNABLE
BLOCKED
TIMED_WAITING
WAITING
TERMINATED
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the advantage on implementing Runnable over extending the Thread class?

A

We can still extend a custom class (not only implement the Runnable). But when we extend the Thread class we can’t extend any further class because Java only supports single-inheritance.

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

How can you stop or terminate a thread?

A
  • wait until run() ends
  • we can also implement a flag variable marked as volatile to end the run loop

-> terminate() or stop() should not be used (deprecated), because we leave the thread in an inconsistent state

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

When is a thread alive?

A

From the moment when the thread has been started with start() the thread is alive. It stays alive as long as the run() method is still running.

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

What’s the purpose of an Executor?

A

To start a Runnable task with the method execute()

Example:

class ThreadPerTaskExecutor implements Executor{ public void execute(Runnable task) {
new Thread(task).start();    
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What can an ExecutorService do, that a simple Executor can’t do?

A

It can control and also stop task execution.

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

What happens when an ExecutorService calls shutdown()?

A

It stopps receiving tasks, waits until all accepted tasks have finished and then terminates the executor.

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

What’s the difference between shutdown() and shutdownNow()?

A

shutdown() waits until all tasks have finished running, shutdownNow() interrupts the running tasks.

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

Which class does ExecutorService extend?

A

Executors

e.g. ExecutorService executorService = Executors.newSingleThreadExecutor();

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

Which statements are true?

a) when a thread in a thread pool dies, a new thread pool is created
b) thread pools are created once
c) thread pools use a set of worker threads that can be reused
d) when a task is submitted the task always goes directly into the thread pool and waits until it can be executed

A

b) and c)
- > thread pools are created only once
- > the set of worker threads can be reused

(tasks don’t go directly into the pool, they first go into the blocking queue, which holds the task until a thread becomes available)

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

Name at least 3 different types of Thread Pools

A
  • SingleThreadPool
  • FixedThreadPool
  • CachedThreadPool
  • WorkStealingPool
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the advantage of a ChachedThreadPool vs a FixedThreadPool?

A

a CachedThreadPool is more dynamic: creates new threads only as needed, can remove threads which aren’t used.
-> ideal for programs using many short-lived asynchronous tasks

17
Q

What are the 3 main methods of the ScheduledExecutorService?

A

schedule()
scheduleAtFixedRate()
scheduleWithFixedDelay()

18
Q

What are the 2 ScheduledExecutorServices provided by the Executors class?

A
  • SingleThreadScheduled Executor -> thread pool size = 1

- ScheduledThreadPool -> uses given thread pool size

19
Q

What are the 2 types of nested classes in java?

A
  • inner classes

- static nested classes

20
Q

Are inner classes static or non-static?

A

non-static

21
Q

When does it make sense to nest a class?

A

When it is only helpful / used in one class. -> more readable code, better encapsulation

22
Q

Can a static nested class directly access the members of the outer class?

A

No, it can only directly access the field if it is static.

23
Q

Can a private inner class be accessed by its outer class?

A

Yes.

24
Q

Can a top-level class be declared “private”?

A

No, only nested / inner classes can be declared private.

25
Q

What are the 2 main differences between Callable and Runnable?

A
  • Callable can return a result

- Callable can throw a checked Exception if unable to compute a result

26
Q

What do the methods Future.isDone() and Future.get() do?

A

Future.isDone()
-> we ask if the result is available

Future.get()
-> we call get() to return the result

27
Q

What are the 2 options to return multiple Futures?

A

Option 1: Store the returned Futures of the submitted callables in a collection by using a for loop, then: for loop through collection with future.get()

Option 2: Use ExecutorService.invokeAll(taskList)
-> store the list that it returns and for loop through the stored list with future.get()

28
Q

What does ExecutorService.invokeAny() do?

A

Waits for the completion of the fastest callable, which doesn’t throw an Exception

29
Q

How do we separate the creation and management from the application logic?

A

By using the Java Executor Framework

30
Q

What are Futures used for?

A

To collect results from callables. They are like a placeholder for the result of the computation, which will be available at a later point in time.