Core Java 9: Multithreading Flashcards

(8 cards)

1
Q

What is multithreading?

A

concurrent/parallel programming–a way to have multiple different sets of instructions running at once

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

Why is multithreading useful?

A

to increase efficiency of application; caveat: for many applications, complexity of multithreading outweighs the benefits

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

Problems Common to Multithreading

A

multithreading introduces a lot of problems because it’s very easy for different sets of instructions to interfere with one another; maybe they’re competing for a resource, maybe operating in different orders

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

Race conditions

A

output of application is dependent on order in which threads execute (unpredictable output, may or may not throw exception/error)

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

Deadlock

A

2 resources acquired in different orders, and threads do not relinquish these resources (a standoff)

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

Starvation

A

a thread acquires a resource and does not release

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

Livelock

A

two threads are competing for resources and are trapped in a feedback loop (hot potato)

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

How can issues in multithreading be addressed?

A
  • Frameworks such as Executor
  • synchronized keyword
  • Thread-safe objects: Collections.synchronizedMap(), Collections.synchronizedList(), StringBuffer, AtomicInteger, etc.
  • Checking/changing the order of locks in threads
  • Fair locks: ReentrantLock with fair policy set to true is an example
  • Design random intervals during which threads attempt to reacquire locks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly