Threads Flashcards

1
Q

How do you create a thread?

A

Subclass the Thread class and override its (empty) run() method

Create a class that implements the Runnable interface with a run() method that does the required work and then give an instance to the constructor of the Thread class.

class MyRunnable implements Runnable {
  public void run() {
    ... Do whatever I want it to
  }
}
-- code in some method somewhere --
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you start a thread?

A

When the Thread.start() method is called (by an existing thread) a new thread of execution is started.

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

How do you put a thread to sleep?

A

A thread can pause itself by calling the Thread.sleep(int time) method

like updating a progress bar every half second

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

How do you make one thread

wait for another thread to complete ?

A

void join() – a thread can call this method on the Thread object that refers to some other thread of execution to stop and wait until that other thread terminates

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

How do you interrupt a thread?

A

Thread objects have an internal flag that indicates whether or not they have received an interruption request by a call to the instance method interrupt().

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

What happens when a thread is

interrupted?

A

Signal is sent by calling interrupted method
If a sleeping thread is interrupted it will wake up and another thread interrupts it.
It throws an Interrupt Execution

Interruption is often used as a mechanism for telling a thread to cancel what it is doing and terminate.

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

-When threads are interleaved, what is a race condition and what is a lost
update?

A

A race condition occurs when two threads access a shared variable at the same time.

The lost update problem occurs when 2 concurrent transactions try to read and update the same data

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

How do you avoid race condition and lost update problems?

A

Wrap code inside synchronized block
synchronized(lockingobject) {
…operations to make atomic…
}

Atomic together because no thread can go inside the Synchronized block if one thread is already there

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

What is the Memory Consistency problem?

A

The changes made by one thread might not be visible to the other threads and they still all have inconsistent views of the same shared data.

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

How is memory consistency problem avoided by Synchronization?

A

synchronization forces the flushing and refreshing of all relevant cache memory (or at least guarantees the same effect if it can be achieved more efficiently).

Next thread will know the value of the variable

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

What does the Volatile Modifier do?

A

1) Guarantees that a read or write of the variable will be atomic on all architectures. This affects ONLY double and long variables,
2) Ensures that all reads and writes of the variable are immediately copied to/from main memory. That is, it guarantees memory consistency for that variable. All threads accessing the variable are guaranteed to see all changes to the variable, even without synchronization.

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

What does the Volatile Modifier not do?

A

Doesn’t have an effect on race conditions

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

What is deadlock and how badly used synchronization
can cause it?

NOT FINISHED

A

A common problem when threads depend on each other to complete some work.

e.g. Thread A waits for Thread B, Thread B waits for Thread A; Both keep waiting forever.

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

How can badly used synchronization

can cause deadlock?

A

Bad use o

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