Concurrency Flashcards

1
Q

Sequential

A

Use the von Neumann architecture. After one task is finished, the program can start the next one. It stops if one of them fails.

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

Parallelism

A

Tasks run on separate threads.

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

Concurrency

A

Multiple tasks can be processed at (not exactly) the same time by switching very fast between processes.

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

Process

A

an executing program

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

Thread

A

one independent task within a process

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

Scheduling

A

-The distribution of thread execution in time slots
- Threads are scheduled separately

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

Context switch for processes

A
  • used by the operating systems to constantly switch between processes
  • saves the current process info and loads the new process info
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are processes and threads connected?

A

A process consists of mini-processes called threads. By default a process only consists of a single ”main” thread, but new threads can be spawned to handle different tasks.

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

How are threads in java created?

A
  • by extending the Thread class
  • by implementing the Runnable interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the states that a thread can have?

A
  • new
  • runnable
  • blocked
  • waiting
  • time-waiting
  • terminated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Thread state: new

A

A thread that has been created, but not in execution.

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

Thread state: runnable

A

Memory for the thread has been allocated and the thread is either running or ready for running

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

Thread state: BLOCKED, WAITING, TIMED WAITING

A

These states all reflect that the thread is currently waiting for something. In the case of BLOCKED,
the thread could be waiting to enter a synchronized method. A thread can enter the WAITING
state if wait() , join() (both without timeout specified) or park() is called. A thread will enter the
TIMED WAITING state if sleep() is called or wait() and join() have a timeout specified.

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

Thread state: TERMINATED

A

A thread that has exited is in this state.

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

What is the life span of a thread?

A

new -> runnable <=> blocked -> runnable -> dead

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

Why does a process have multiple threads?

A

Switching between processes is expensive because it needs to do a context switch. It’s cheaper to switch between threads because they share execution information.

17
Q

What is the difference netween run() and start() methods ?

A

Calling run() will only execute the code
within the run method without actually starting the thread. By calling start() the code in the run() method will be executed.

18
Q

Why choose implementing Runnable instead of extending the Thread class?

A
  • code is more loosely coupled when using an interface vs extending a class
  • extending Thread limits us from extending the class from another class
  • pass the same Runnable object to different threads
  • we are (most likely) not overriding anything from the Thread class
19
Q

What is ExecutorService?

A

Another popular way for creating and managing threads; it works with a thread pool; the ExecutorService object is created using
the Factory pattern

20
Q

What is a thread pool?

A

A collection of threads where each thread is waiting for a task

21
Q

What are the kinds of tasks that we can submit to the executor service?

A

Runnable and callable

22
Q

Why use executors instead of manually creating threads?

A

Executors have more control over the execution context. For a few threads it is not necessary to use executors, but for an application with a lot more threads it makes sense to separate thread management from the rest.

23
Q

What does thread safety refer to?

A

It refers to multiple threads trying to access (read or write) the same resource at the same time. The sections that can cause issues are called critical sections.

24
Q

How to ensure thread safety?

A

The ‘synchronized’ keyword(for a method or block of code) - using a lock, it ensures that no other thread can call the method while it is already in use by a thread.

25
Q

What are atomic operations?

A

Operations where everything happens at once (in contrast to, for instance, ‘++’ which consists of three operations under the hood) (in java: reading and writing of reference and primitive variables, except for long and double)

26
Q

How do you shut down an Executor? (It is important to do so)

A

You can do that by calling shutdown() or shutdownNow(). The first one makes the executor to stop accepting new tasks and shut down when all tasks are complete, whereas the last one interrupts the currently running tasks.