Module 21 - Threads (3).pptx

What is the purpose of a condition object?

Question

Releases lock temporarily, regains it later

Answer

1/27
T

Created by

Tiago Bini

Sign up for Studygenie 100% free

Join 10,000+ students acing their exams with Studygenie

Questions in this set (27)

What is the purpose of a condition object?

Releases lock temporarily, regains it later

To what does each condition object belong?

Specific lock object

What should a thread call on a condition object if a test is not fulfilled?

Call await

What does calling await do to the current thread?

Makes current thread wait

What does calling await allow?

Another thread acquires the lock object

What happens to other threads that call deposit while another thread is executing deposit?

Blocked until withdraw exits

What is the purpose of a lock object?

To control threads that manipulate shared resources

What must another thread execute to unblock a waiting thread?

signalAll

What are the two types of locks mentioned?

Object locks and Class locks

What does signalAll do?

Unblocks all waiting threads

What does signal do?

Randomly picks one thread and unblocks it

Why is the sufficientFundsCondition object an instance variable and not a local variable?

Calls to await and signal/signalAll must be made to the same object

When is a waiting thread reactivated?

Another thread has called signalAll or signal

How do you obtain a condition object?

newCondition method of Lock interface

What does an object lock control?

Controls whether a method can be run for a particular object

What must a method do to run when using object locks?

Acquire the lock for the object

When does a method release the object lock?

Releases the lock when method ends

What happens when a thread has a class lock?

Only thread that can execute any calls of methods that require a lock

What interface is used for locks in Java?

Lock interface

Which lock class is most commonly used in Java?

ReentrantLock

From which Java version are locks a feature?

Version 5.0

How is code that manipulates shared resources handled?

Surrounded by calls to lock and unlock

What happens if code between lock and unlock throws an exception?

Call to unlock never happens

How can you ensure unlock is always called, even if an exception is thrown?

Place call to unlock into a finally clause

What happens to a thread that calls lock while another thread owns the lock?

Temporarily deactivated

What periodically reactivates a thread so it can try to acquire a lock?

Thread scheduler

If you construct two BankAccount objects, how many lock objects are created?

Two