Threads Flashcards

1
Q

How does single core systems have multiple threads?

A

Time-Slicing.

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

What is a thread?

A

A lightweight process. A process is used for heavyweight tasks like executing an application. Threads exist in a process. Threads share process resources.

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

What is multi-threading?

A

Example: word prints docmnent but also accepts user input typing.

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

What are critical sections?

A

Code that modifies data structures shared by multiple threads.

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

What is synchronization?

A

When code of one thread is in critical section it’s important another thread isn’t allowed in critical section. Signaled and unsignalled.

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

In Java, how to start a thread?

A

Provide runnable object (implements Runnable). Must have run() method. Runnable object then passed to Thread constructor. Must also invoke Thread.start().

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

How do you make thread sleep?

A

Thread.sleep(msec);

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

What is an interrupt? How is interrupt done?

A

A thread should stop what it’s doing and do something else. Use try catch for interrupted exception and return out of catch block.

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

How do you get name of current thread?

A

Thread.currentThread().getName();

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

How do you check if a thread is alive?

A

t.isAlive() - it returns a Boolean

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

How to wait for thread t? How to interrupt thread t?

A

t. join(msec);

t. interrupt();

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

What is thread contention?

A

When two or threads try to access the same resource simultaneously and as a result they execute more slowly or not at all.

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

How to make a method synchronized?

A

public synchronized void increment(){}

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

Write a synchronized statement:

A

synchronized (this){}

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

What is deadlock?

A

Two or more threads blocked forever, waiting for each other.

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

What is starvation.

A

Other greedy threads make resources unavailable for a thread.

17
Q

What is livelock?

A

Two threads unable to make progress as they keep responding to each other’s responses. Like two people meeting in corridor.

18
Q

What are guarded blocks? ie using wait() method?

A

while(!someCondition){
try { wait(); }
catch (InterruptedException e) {}
}} print }

wait() does not return until another thread issues notification that some event has occurred.

19
Q

How does another thread let other threads know it has squired look and some important event has happened?

A

notifyAll();