Concurrency Flashcards

1
Q

What are the 6 stages of a threads lifecycle?

A
Ready to run, 
running, 
waiting, 
sleeping 
blocked on I/O or synch
Dead
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is event dispatch?

A

The gui thread within java

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

When does a single thread die?

A

When its run method returns or .stop is called on it

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

What class do the .notify, and .wait methods belong to?

A

Belongs to the object class

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

What object do the sleep and, start and yield belong to?

A

Thread object.

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

When is a class thread safe?

A

If it behaves correctly when accessed by multiple threads. Regardless of the scheduling or interleaving of those threads by the runtime. With no extra synchronisation on the part of the calling code.

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

Why should you avoid sharing states between threads wherever possible?

A

Sharing states leads to race conditions and deadlock?

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

What are the ways to create a new thread?

A
Calling .run() on any of the following:
A new anonymous thread class, 
A new thread with a lambda inside. 
New thread subtype with the run class overridden, 
New runnable implementation passed as a parameter to a new Thread.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Implementing runnables vs extending threads? Whats the difference.

A

Inheritance should be used when you want to override existing functionality, what existing behaviour are we trying to override in thread? Instead we seek to extend the existing behaviour by giving it something to run. Furthermore, implementing runnables creates loose coupling in our program and allows our runnable to inherit something else.

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

What is a race condition?

A

A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Thread scheduling means that their order is random. The random timing and simultaneous access can result in unexpected results.

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

What are check then act and read then write.

A

Neither of these operations are atomic, check then act can make the condition that was checked become outdated midway throughout execution.
Whilst in read then write two threads can read a value at the same time causing the wrong write value.

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

Why can even primitives behave unexpectedly?

A

The JVM caches values for periods of time. Meaning that the change in value may not be a publicly available resource.

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

What is write back caching?

A

When stuff is placed in the cache and then eventually put into main memory in batches.

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

What is write through caching?

A

When the processor is told to put straight from the cache into main memory.

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

Which is faster out of write through and write back caching? Which is safer?

A

Write back is faster. Write through is safer.

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

What is a thread?

A

A flow of control within a program.

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

What are the advantages of threads?

A
  • Reactive/realtime system.
  • Responsive to user input.
  • Concurrent server requests.
  • Take advantage of multicores/processors.
  • Supports object decoupling
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What methods for thread control are deprecated?

A

.stop, .suspend and .resume. Because they suddenly seize control away from the thread mid execution.

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

What are daemon threads?

A

They are essentially like background support task threads. Once all non daemon threads die, then all daemons die aswell and the program finishes.

20
Q

Can primitives be locked?

A

No

21
Q

What can be synchronized?

A

Methods or blocks.

22
Q

What does synchronization do?

A

When threads are executing and they encounter a synchronized method or block, in order to execute the following code, they most own the lock to a specificied object.

23
Q

What does synchronization do?

A

When threads are executing and they encounter a synchronized method or block, in order to execute the following code, they must own the lock to a specified object.

24
Q

What is an intrinsic lock/monitor lock/monitor?

A

Every object has an intrinsic lock associated with it. Threads which need exclusive access to an objects fields/methods must own the intrinsic lock for that respective object.

25
Q

What happens when a thread holds the monitor for an object?

A

All other threads which wish to access the object must wait until the monitor lock has been released to them.

26
Q

What happens when static methods are synchronized?

A

A separate monitor exists for the entire class which must be acquired to utilise any synchronized static fields.

27
Q

What happens when static methods are synchronized?

A

A separate monitor exists for the entire class which must be acquired to utilise any static fields.

28
Q

What is reentrant synchronization.

A

Threads cannot acquire a lock held by other threads. They can reacquire the same lock repeatedly. When doing so, the number of lock releases must equal the lock acquisitions for the lock to be properly released.

29
Q

What causes deadlock

A

When two threads are both waiting for each others locks in order to continue, neither thread can progress, causing them to wait indefinitely.

30
Q

What causes deadlock

A

When two threads are both waiting for each others locks in order to continue, neither thread can progress, causing them to wait indefinitely.

31
Q

What is the modern toolbox for concurrency?

A

Java.util.concurrent

32
Q

Do atomic classes inherit from their similarly named classes?

A

No. Though AtomicInteger extends Number.

33
Q

What does the Atomic set of classes do?

A

Supports lock free thread-safe programming on single variables. Each object represents a corresponding primitive and contains helper methods.

34
Q

What does java.util.concurrent.locks do?

A

Provides an alternative model of synchronization within Java, essentially it expands the things that locks can do. it can replicate any functionality from block structured concurrency.
New ideas include:
-Add different types of locks (such as reader and writer locks).

  • Not restrict locks to blocks (allow a lock in one method and unlock in another).
  • If a thread cannot acquire a lock (for example, if another thread has thelock), allow the thread to back out or carry on or do something else—a try-Lock() method.
  • Allow a thread to attempt to acquire a lock and give up after a certain amountof time
35
Q

What is a countdown latch?

A

A synchronization pattern which allows multiple threads to agree on a minimum amount of preparation to be done before any thread can pass a synchronization barrier.

36
Q

How is a countdown latch implemented?

A
You must use
new CountDownLatch(int)
then use cdl.await() which will cause all threads to wait until calls to .countdown means that cdl reaches 0.
37
Q

How does ConcurrentHashMap work?

A

Placing things in a hashmap only really requires you to lock the bucket being altered.

38
Q

Why is ConcurrentHashmap useful?

A

Increased performance and thread safety without the any explicit synchronization of blocks.

39
Q

What is java.util.concurrents alternative to an arraylist?

A

CopyOnWriteArrayList
If you look at the underlying array reference you’ll see it’s marked as volatile. When a write operation occurs (such as in the above extract) this volatile reference is only updated in the final statement via setArray. Up until this point any read operations will return elements from the old copy of the array.

The important point is that the array update is an atomic operation and hence reads will always see the array in a consistent state.
Can cause low performance when many write operations occur and can also cause stale values to be read in an iterator.

40
Q

When is CopyOnWriteArrayList bad?

A

It is bad when write operations outweigh read operations. This is because it must copy the underlying array every time which affects performance.

41
Q

What are the benefits of the queue?

A

The queue provides a simple and reliable way to dstribute processing resources to work units.

42
Q

What is java.util.concurrents implementation of a queue?

A

The blocking queue. When trying to put() to the queue, it will cause the putting thread to wait for
space to become available if the queue is full.
When trying to take() from the queue, it will cause the taking thread to block
if the queue is empty

43
Q

How does a transfer queue differ to a blocking queue?

A

It just has one extra operation transfer, this directly transfers a work item to a waiting receiver thread

44
Q

What does the callable interface do?

A

Has one method call, which runs some code and returns a value.

45
Q

What does the future interface do?

A

It is used to represent an asynchronous task, it uses .get() to try and get a result or otherwise block until it is ready. Also has .cancel if you don’t want to wait anymore.

46
Q

What is FutureTask interface?

A

It is a combination of the future and runnable interface.

47
Q

What is the ScheduledThreadPoolExecutor?

A

It is the backbone of thread pool classes