ECM2414 Concurrency Flashcards
Concurrency concept
To be able to perform multiple processes at the same time - including the potential interaction between them
- Multiple tasks at the same time is sometimes called parallelism
Why concurrency
- Allowing programs to interact with other systems/users
- Multi-core systems
- Systems can achieve more processes in the same time length
- Speed is key
- Productivity increases
- Consistency - real time responses
Threads
Can be treated as lightweight processes
Threads exist within a process
Processes can be considered as these big boxes. Many threads running at the same time in a process.
Why threads and not separate programs?
Shared memory space, so threads can access different segments of a program/process so said process is executed faster. A way to give a class or object the right of usage of a particular resource at a given time
The OS can run threads in parallel, how..
- Virtually on a single processor
○ On core of the system running numerous threads, not visible to users, its very fast.
○ IPC communication schedules what comes first between all the threads on the same processors - Actually on a multi-core processor
Nondeterministic processes
in sequential task management, tasks can happen one after the other
Problems: Time issues, Complexity, if the ensuing task,
memory address issues: everything relies on memory
We cannot accurately say our system is efficient as memory usage and complexity cannot be determined
Threads in Java
Can be created in 2 ways
- Using and extending the Thread class
- Using the Runnable interface
New thing to worry about - liveness
- Concerned that code performs correctly in time - i.e that good things do happen eventually
- What level of “eventually” is acceptable
current use thread lifecycle methods
Package java.lang
public class Thread implements Runnable
start
run
sleep
isAlive
interrupt
isInterrupted
interrupted
join
How do i stop a thread
Setting a flag:
Loop will exit (and thread will stop) on the next iteration after done is set to tru
Interrupting:
- As above, when isInterrupted() is called for this thread, it will stop
- Except that if the thread is sleeping/waiting will throw Interrupted Exception and immediately return from run()
Race conditions
Data synchronisation between threads can be an issue
When two threads try to access/change data at the same time it’s
known as a race condition
Synchronisation
Simply add the synchronize keyword to the methods in the class
When one thread is executing a synchronized method for an
object, all other threads that invoke synchronized methods for the
same object block suspend execution until the first thread is done with the object.
Atomic actions
In programming, atomic actions are self-contained, they cannot be stopped in the middle, they either happen or they don’t
Atomic access can help here:
- Java primitive data types: byte, short, int, long, float, double, boolean, char
- reads and writes are atomic for most primitive variables (all types except long and double)
- reads and writes are atomic for all variables declared volatile, including long and double variables
Deadlock
Thread 1 needs the resource held by thread 2
Deadlock, having a thread lock its resources, using keyword lock to shield off its resources
Starvation
Where a thread can’t gain regular access to shared resources because another thread is frequently calling the object’s methods