Concurrency Flashcards
Sequential
Use the von Neumann architecture. After one task is finished, the program can start the next one. It stops if one of them fails.
Parallelism
Tasks run on separate threads.
Concurrency
Multiple tasks can be processed at (not exactly) the same time by switching very fast between processes.
Process
an executing program
Thread
one independent task within a process
Scheduling
-The distribution of thread execution in time slots
- Threads are scheduled separately
Context switch for processes
- used by the operating systems to constantly switch between processes
- saves the current process info and loads the new process info
How are processes and threads connected?
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 are threads in java created?
- by extending the Thread class
- by implementing the Runnable interface
What are the states that a thread can have?
- new
- runnable
- blocked
- waiting
- time-waiting
- terminated
Thread state: new
A thread that has been created, but not in execution.
Thread state: runnable
Memory for the thread has been allocated and the thread is either running or ready for running
Thread state: BLOCKED, WAITING, TIMED WAITING
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.
Thread state: TERMINATED
A thread that has exited is in this state.
What is the life span of a thread?
new -> runnable <=> blocked -> runnable -> dead