____________ are utilized to control the access to an object especially in multi threaded programming

Options
- Asynchronized methods
- Serialized methods
- Synchronized methods
- Both a and c


CORRECT ANSWER : Synchronized methods

Discussion Board
Synchronized Method

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors. The following things can occur due to this:
1. If an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.
2. It is not possible for two instances of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object then other threads or methods block itself once the process of first thread execution is finished.
3. On the exit of synchronized method, it automatically creates a happens-before relationship with any other instance running of a synchronized method for the same object. This allow the changes to the state of the object that are visible to all threads.

For example:

public class SynchronizedCounter {
private int c = 0;

public synchronized void increment() {
c++;
}

public synchronized void decrement() {
c--;
}

public synchronized int value() {
return c;
}
}

Rohit Sharma 08-10-2014 06:07 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement