Thursday, July 18, 2013

10. What is Synchronized in Java ?

In Java "Synchronized" appears for synchronized methods and synchronized statements.

To make a method synchronized, simply add the synchronized keyword to its declaration:

public class SynchronizedCounter {

    private int c = 0;

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

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

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

No comments :

Post a Comment