Concurrency Flashcards
What are the conceptual from of tasks?
A process is started when the Java program is first ran, causing the OS to allocate it resources. It starts with one executing three
One or more threads exist within a process and can execute to complete a task.
A task is a set of program instructions as defined by the written code.
What are the measures of success for a concurrent application?
Efficiency - how little computing resources can be used
Responsiveness - how soon can events be responded to
Task separation - allows for interleaving i.e. stops one task blocking the others
What is the difference between threads and processes?
Both get their own method call stack and program counter (keep track of which line of code). However, threads share the memory heap wiht threads from the same process.
How can threads be paused to allow execution of other threads?
Either voluntarily in “cooperative scheduling” in which they yield when able to.
Otherwise the SO interrupts them - “preemptive time slicing”
How do Java threads relate to OS supported threads?
OS-supported threads run each thread on the same CPU / core. Threads from the same process may run on different CPUs.
Java threads run as OS-supported threads, but the JVM typically switches them between different OS-supported threads over time.
What is the basic lifecycle of a thread?
They start as “new” when created by become “runnable”.
When runnable, the OS manages the transition between “ready” and “running”
Other states it may be in include waiting, timed waiting (wait but with timeout), blocked (waiting to acquire a lock etc.) and terminated (it is done)
How is the transition from ready to running managed?
This is all hidden from the JVM - both are simply encompassed by Runnable.
The OS determines when to “dispatch” the thread and hence move it on to the processor. This is called “thread scheduling”
How are threads created?
By defining a new class that extends the Thread class. Specifically the run() method must be overridden.
An instance of that class is then created and then executing by calling .start() on it.
What parameter must thread constructors have?
The name of the thread. Optionally it can also have a custom runnable.
How can threads be created using the Runnable interface?
Define a new class that implements the Runnable interface.
Implement a run() with the task to be executed.
Instantiate a new Thread with the custom runnable as the constructor, as well as the thread name. Execute this thread.
How can the current thread be accessed?
With Thread.currentThread().
For example, the name of the current thread can be retrieved with Thread.currentThread().getName()
When is it best to implement Runnable versus extend Thread?
When it makes sense to extend from a different class (multiple inheritance is not allowed)
The Runnable instance can be used over multiple threads, helping reduce memory consumption.
What are the two basic types of thread?
Daemon threads provide support to non-daemon threads. These do not help keep the application alive.
Non-daemon threads are the key threads of the application such as the main thread (for console applications) or the event dispatch thread (GUI).
If all non-daemon threads have terminated then the daemon threads, and hence entire application, is terminated.
When a thread is created, what type is it by default?
A user thread (non-daemon)
What are daemon and non-daemon threads also called?
Daemon - background, service
Non-daemon - user threads
How can threads be made daemon or not?
With myThread.setDaemon(true/false). This must be done BEFORE the thread is started.
To determine if a thread is daemon or not, use .isDaemon
What is an alternative way of executing Runnable objects?
This object can be passed to an ExecutorService which manages a pool of reusable threads behind the scenes.
How are executor services used?
They are created using the Executors.newCachedThreadPool() static method.
Runnable can be executed using service.execute(runnable)
How can an executor with a specific number of threads be created?
Use Executors.newFixedThreadPool(int nThreads)
What methods do executors provide to manage termination?
shutdown() - initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks are
shutdownNow() - attempts to stop all executing tasks
awaitTermination() - blocks the invoking thread until all tasks have been completed / timed out
What are race conditions?
When multiple threads are accessing mutable shared data at the same time
What are atomic operations?
Those that operate on share memory as a single step from the perspective of other threads.
Therefore, intermediate values cannot be messed with
What are the methods of AtmoicInteger?
get()
getAndIncrement()
getAndAdd(int delta)
How can methods be made thread safe?
With the “synchronised” keyword i.e. public synchronised void doSomething() { }
This allows only one thread at a time to execute the method. Any other thread will be blocked.