Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

What is an objects state? - Data stored in state variables such as instance OR static fields.

- Includes fields (static/instance) from other dependent objects. - Encompasses any data that can affect its externally visible behavior. What does a Shared variable mean? - A variable that could be accessed by multiple threads. What do you mean by mutable? - The value of the object state could change during its life time. What is thread-safety? - Protecting mutable data from uncontrolled concurrent access. If not protected, the data will be corrupted. It is a property of how the object is used in a prog, not what it does. How can you make an object thread-safe? By synchronizing the access to an objects mutable state. By not sharing the state variables (instance / static fields) across threads By making the state variables immutable

When do you say a class is thread-safe? If it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime env, and with no additional synchronization or other coordination on the part of the calling code.(Thread-safe classes encapsulate any needed synchronization so that clients need not provide their own)

How do you achieve synchronization in Java? using synchronized keyword using volatile variables explicit locks Atomic variables

What are atomic variables? The java.util.concurrent.atomic package defines classes that support atomic operations on single variables. All classes have get and set methods that work like reads and writes on volatile variables.

What OO techniques help in designing thread-safe classes? - Encapsulation : The class encapsulates its own state - Immutability - Clear specification of invariants Are stateless objects are always thread-safe? - Yes Is the increment operation, ++count, a single (atomic) operation? - No. It will be not executed as a single, indivisible operation. It is a read-modifywrite operation with three discrete operations: a. Fetch the current value b. Add one to it c. Write the new value back What is a race-condition? A Race condition occurs when 2 or more threads are able to access shared data and then they try to change it at the same time. Because the thread scheduling algo can swap b/w threads at any point, you dont know the order at which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are 'racing' to access/change the data. The most common type of race-condition is "check-then-act" (e.g. "check" if the value is X, and then "act" to do something that depends on the value being X) and another thread does something to the value in between the "check" and the "act". A common idiom that uses check-then-act is lazy initialization.

How do you avoid race condition? To ensure thread safety, check-then-act operations, read-modify-write operations must always be atomic.
What's harder, synchronizing 2 threads or synchronizing 1000 threads? You could make the case that synchronizing 2 threads correctly is in fact harder than doing it for 1000, because if you have a race condition, it will usually manifest very quickly with 1000 threads, but not so with only 2. But on the other hand, synchronizing 1000 threads without running into lock contention issues is much harder than when there are only 2. The real answer is "synchronizing threads is hard in various ways, period."

Synchronizing a thousand threads is just as easy as synchronizing two threads: just lock access to all important data. Now, synchronizing a thousand threads with good performance is more difficult.

You might also like