Which three code segments should you insert in sequence at line 09?

You are developing an application that includes a class named Warehouse. The Warehouse class includes
a static property named Inventory- The Warehouse class is defined by the following code segment. (Line
numbers are included for reference only.)

You have the following requirements:
-Initialize the _inventory field to an Inventory instance.
-Initialize the _inventory field only once.
-Ensure that the application code acquires a lock only when the _inventory object must be instantiated.
You need to meet the requirements.
Which three code segments should you insert in sequence at line 09? (To answer, move the appropriate
code segments from the list of code segments to theanswer area and arrange them in the correct order.)

You are developing an application that includes a class named Warehouse. The Warehouse class includes
a static property named Inventory- The Warehouse class is defined by the following code segment. (Line
numbers are included for reference only.)

You have the following requirements:
-Initialize the _inventory field to an Inventory instance.
-Initialize the _inventory field only once.
-Ensure that the application code acquires a lock only when the _inventory object must be instantiated.
You need to meet the requirements.
Which three code segments should you insert in sequence at line 09? (To answer, move the appropriate
code segments from the list of code segments to theanswer area and arrange them in the correct order.)

Answer:

Explanation:
if(_inventory == null)
lock(_lock)
if(_inventory == null) _inventory = new Inventory();
This will only instantiate the inventory if there is no current value.



Leave a Reply 5

Your email address will not be published. Required fields are marked *


MarcoJacob

MarcoJacob

1.) block 4
2.) block 3
3.) block 5

second “if(…)” is necessary to be sure that _inventory isn`t instantiate in the time between

Kim Tae Hong

Kim Tae Hong

by “-Ensure that the application code acquires a lock only when the _inventory object must be instantiated.” condition, 1.)block 4 -> block 2

DrDre

DrDre

No it means it only needs the lock when the object is currently empty

Herman McConnell

Herman McConnell

Really? So, if two threads will try to access field that is not null in the same time – it will be just fine? Common.

Herman McConnell

Herman McConnell

3 and 5, or 4->3->5 but I’m not really sure about double if null condition.