Week 2 Flashcards

1
Q

What is Concurrency?

A

To be able to perform multiple processes at the same time - including the potential interaction between them

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

What is multiple tasks at the same time sometimes called?

A

Parallelism

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

Why are we interested in Concurrency?

A
  1. Allowing programs to interact with other systems/ users
  2. Multi-core Systems
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are Threads?

A

Treated as Lightweight Processes

Threads exist within a process

Many threads running at same time in a particular process

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

What is Multi-Threading?

A

OS can run threads in parallel:

Virtually on a Single Core Processor

Actually on a Multi Core Processor

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

What is a problem with Threads?

A

Liveness

Liveness threads refer to a thread safety issue that occurs when a program doesn’t produce a final result

Liveness issues can have serious consequences, such as deadlocks that prevent a program from running further

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

What are the 2 ways to create Threads in Java?

A
  1. Using (and extending) the Thread class
  2. Using the Runnable interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 2 Ways to Stop a Thread in Java?

A
  1. Setting a flag:

public void run() {
while (!done) {
//…
}
}

Loop will exit (and thread will stop) on the next iteration after done is set to true

  1. Interrupting:

public void run() {
while (!isInterrupted() ) {
//…
}
}

When isInterrupted() is called for this thread, it will stop

Except that if the thread is sleeping/ waiting will throw InterruptedException and immediately return from run()

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

What is a Race Condition?

A

Issue with Threads

When 2 or more Threads try to access same resources it is called a Race Condition

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

How do you fix Race Conditions?

A

Synchronisation

Simply add the synchronize keyword to 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 first thread is done with the object

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

What is Statement Synchronisation?

A

Where you synchronise a set of statements, not necessarily the whole method

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

What are Atomic Actions?

A

Self contained

We cannot interrupt an atomic process at any given time

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

What is Deadlock?

A

Deadlock is a situation where two or more processes are unable to proceed because each is waiting for the other to release a resource

This creates a cycle of dependency that halts all progress, causing the system or the involved processes to become stuck indefinitely

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

What is Starvation?

A

Where a thread can’t gain regular access to shared resources because another thread is frequently calling the object’s methods

Greedy Thread and Starved Thread

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

What is Livelock?

A

Like Deadlock

Except it occurs when 2 Threads are too busy responding to one another to do work

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

What does “void wait();” do?

A

waits for condition to occur, must call from synchronised method/ block

17
Q

What does “void wait(long timeStep);” do?

A

Waits for condition for timeStep milleseconds, then returns, mustr call from synchronised method/ block

18
Q

What does “void notify();” do?

A

Notifies a waiting thread a condition has occurred, must call from synchronised method/ block

Allows Thread communication

19
Q

What does the wait() method do?

A

Releases the lock and regains it using notify()

20
Q

What does the sleep() method do?

A

Allows the Thread to go into inactive mode for a specific length of time

21
Q

What is the difference between wait and sleep?

A

wait:

Must occur in block synchronised to monitors object

Releases the monitor(lock) when called and doesnt sacrifice remainder of its timeslice

Will wake up when notified via a notify call

sleep:

Does not need to occur in a synchronised block

Retains the monitor(lock) when called and sacrifices the remainder of its timeslice

Sleeping thread can only be woken up by an interruption (not by notify)

22
Q

What does the yield() method do?

A

causes currently executing thread object to pause and allow other threads to execute

If no other threads of same priority need to execute, execution of this thread continues

Identifies current thread as doing something not particulary important

23
Q

What does the join() method do?

A

Simple

Allows one thread to wait for the completion of another

24
Q

What are the properties of Locking?

A

In synchronised the lock exists but isn’t seen

Possible to create, move and destroy locks through the Lock object

Using Lock interface we can manage locks and:

  1. Two or more objects can share a lock
  2. One object can have multiple locks
25
Q
A