You need to implement the UpdateScore() method

You are adding a public method named UpdateScore to a public class named ScoreCard. The code region
that updates the score field must meet the following requirements:
it must be accessed by only one thread at a time.
it must not be vulnerable to a deadlock situation.
You need to implement the UpdateScore() method. What should you do?

You are adding a public method named UpdateScore to a public class named ScoreCard. The code region
that updates the score field must meet the following requirements:
it must be accessed by only one thread at a time.
it must not be vulnerable to a deadlock situation.
You need to implement the UpdateScore() method. What should you do?

A.
Place the code region inside the following lock statement:
lock (this)
{

}

B.
Add a a private object named lockObject to the ScoreCard class. Place the code region inside the
following lock statement:
lock (lockObject)
{

}

C.
Apply the following attribute to the UpdateScore() method signature:
[MethodImpl(MethodImplOptions.Synchronized)]

D.
Add a public static object named lockObject to the ScoreCard class. Place the code region inside the
following lock statement:
lock (typeof(ScoreCard))
{

}

Explanation:
http://blogs.msdn.com/b/bclteam/archive/2004/01/20/60719.aspx
comment:
A locks entire ScoreCard and we don’t want that
D lock all object of type ScoreCard
C it should work but B is much more preferred



Leave a Reply 0

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