Concurrency Lecture 4 Flashcards

1
Q

What does the start() method do?

A

It initialises and allocates memory for a new thread. It also calls the run() method tied to that thread, making the thread eligible to be run by the JVM.

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

What are the two types of threads?

A

Daemon and non-daemon.

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

What is the difference between daemon and non-daemon threads?

A

The JVM shuts down when all non-daemon threads have executed and have exited.

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

How do you create a daemon thread?

A

By calling the setDaemon() method on the created thread, but before it has started.

thread. setDaemon();
thread. start();

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

Name the 6 thread states.

A

In no particular order:

  1. New
  2. Terminated
  3. Blocked
  4. Waiting
  5. Timed Waiting
  6. Runnable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you determine the start of a thread?

A

The isAlive() method returns true if the thread it is called on has not reached its terminated state, otherwise it returns true.

The getState() method returns the state of the method it is called on as an enumerated data type as one of the states.

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

What methods can make a thread go into a waiting state?

A
  • wait() without a timeout
  • join() without a timeout
  • Lock Support.park
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What methods can make a thread go into the timed Waiting state?

A
  • sleep()
  • wait() with a timeout
  • join() with a timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What do the threads that are automatically created in the system group do?

A

Mostly managing object references.

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

What is a lock condition?

A

A condition created by the lock at the start of the object, which can be used to make a thread wait for something to happen, allowing another thread to run and possible notify the awaiting thread that the condition has been met.

E.g. Insufficient funds in a bank, so await for a thread that runs the top up method to signal it.

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

What code do we write to use a condition for a lock?

A

Instance:
private Condition cond;

Constructor creation (Creates new condition for the fundsLock lock, assume we have created) :
cond = fundsLock.newCondition();
In 'withdraw' method:
Boolean stillWaiting = true;
// lock
... 
while (funds < amount) {
      If (!stillWaiting) {
            Thread.currentThread().interrupt();
      } 
      stillWaiting = cond.await(10,        TimeUnit.SECONDS);  // Condition waits 10 or until signalled by other thread. 
} 
... 
// unlock
In 'deposit' method:
// lock
...
// Critical code
cond.signalAll();
// unlock
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the new state for a thread.

A

A thread in this state is a thread which has been created (instantiated) but not yet started.

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

Explain the terminated state.

A

A thread is in this state when it’s run() method has ended.

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

Explain the blocked state.

A

A thread is in this state when it waits to aquire a lock

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

Explain the waiting state.

A

When a thread is waiting for an action by another thread. For example, it has invoked a wait method without a timeout.and is now waiting.

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

Explain the timed Waiting state.

A

When the thread is waiting for a specified maximum amount of time. For example, it has called a wait method with a timeout.
Thus a thread in this state will not wait indefinitely.

17
Q

Explain the runnable state.

A

When the threads run() method has been invoked it is in the runnable state (The run method is called by the start method). The thread state changes from new to runnable. A running thread is still in the runnable state.

A thread in the runnable state is a thread that is eligible to be run by the scheduler.