Java Threads Flashcards

1
Q

Which library do we use to implement threads in Java?

A

java.lang.thread

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

What does the start() method do

A

method called to spawn a new thread

causes JVM to call run()

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

What does the interrupt() method do

A

freeze and throw exception to thread

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

Does calling run() start a thread?

A

NO! only start() starts a thread

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

how do you get the Thread ID

A

t.getId()

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

How do you set a thread name?

A

t.setName(…..)

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

How do you set Thread priority and what priorities can be set?

A

t.setPriority() Threads can have priority 1 through 10

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

how do you check if a thread is terminated?

What other states can be checked?

A

if (t.getState() == State.TERMINATED)

Other states:
new, runnable, blocked, waiting, time waiting or terminated

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

What is Coarse-grained locking?

A

Wrapping everything that remotely resembles a critical section into a lock

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

What is hand-over-hand locking in a linked list?

A

When a thread traverses the data structure, it lock each node when it first visits it and releases it once it has acquired the lock for the next node.

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

What is optimistic locking in the sense of a sorted linked list?

A

We itreate the data structure without taking any locks. Once we have found the required elements we lock them and check if everything is still correct.
If the state has changed while traversing and the operation cant be performed anymore we start over.

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

In optimistic locking we traverse the sorted linked list until reaching the required node and lock it. We only execute if the state hasnt changed. What conditions do we use to check the state? There are 2

A

1) The node to be operated on is stil reachable

2) the nodes predecessor is reachable and points to the node to be operated on

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

What do we need when we need to implement a .wait() somewhere? 2 things

A

.wait() is always in a synchronized block!

.wait() is always in a while loop!

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

What ca we say about the volatile keyword

A

when a variable is set as volatile it’s like telling the compiler that the variable is important and that everything that happens before that variable is used is visible to all threads.

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