Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

synchronization block

>>When a synchronized block is executed, its object is locked and it cannot be called by any other code until the lock is freed.

synchronized void first();
synchronized void second();
There is more to obtaining the benefits of synchronization than placing the keyword synchronized before a block of code. It must be used in conjunction with code that manages the lock on the synchronized code .




I read above synchronization and explanation from link
http://www.jchq.net/certkey/0703certkey.htm

It is not clear to me in terms why the object is locked when we apply synchronization on individual specific block. Also following is not clear to me
>>>There is more to obtaining the benefits of synchronization than placing the keyword synchronized before a block of code. It must be used in conjunction with code that manages the lock on the synchronized code .


Any ideas, resources, links, sample code highly appreciated. thanks in advance.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
dpearson

When synchronize is used on a method like:

synchronized void first();

this means it's holding a lock on the object that has the "first" method.  It's the same as writing:

void first() {
    synchronize(this) {
        ... Do the operation
    }
}

Only one thread can hold a lock on an object at a time, so it stops a second thread from manipulating "this" object at the same time.

>>>There is more to obtaining the benefits of synchronization than placing the keyword synchronized before a block of code. It must be used in conjunction with code that manages the lock on the synchronized code .

The second part of synchronize is that it's important in telling Java how to correctly manage memory on a multiprocessor machine.  Most people are unaware of this, but the synchronize keyword is very important for ensuring that your code runs correctly when there are multiple CPUs in use.  The full explanation is lengthy, but the key concept is to always put in a synchronize statement when two threads and interacting and you want each thread to see the changes made by the other.

Doug
Avatar of gudii9

ASKER

>>>
synchronized void first();

this means it's holding a lock on the object that has the "first" method.  It's the same as writing:

void first() {
    synchronize(this) {
        ... Do the operation
    }
}


how both are same. What 'this' represents here.
yes they r both the same
"this" refers to the object whose method you are inside:

String getName() { return "Hello" ; }

void whatIsThis() {
    // "this" refers to the current object
    System.out.println(this.getName()) ;

    synchronized(this) {
        // Two threads won't try to modify "this" object at the same time.
    }
}

Putting the keyword "sychronized" on the method is just shorthand for writing "sychronized(this)" within the method.

I hope that helps.
Avatar of gudii9

ASKER

>>>he Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements. The more complex of the two, synchronized statements, are described in the next section. This section is about synchronized methods.
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;
    }
}
If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:
First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
Note that constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.

Warning:  When constructing an object that will be shared between threads, be very careful that a reference to the object does not "leak" prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the line
instances.add(this);
to your constructor. But then other threads can use instances to access the object before construction of the object is complete.
Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. (An important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-synchronized methods, once the object is constructed) This strategy is effective, but can present problems with liveness, as we'll see later in this lesson.




if you have more synchronized methods as in above example like decrement(), increment() which increase or decrease value of counter will synchronizing each method like this is useful at all?


other thread might decrement while original, first thread might increament same count which could corrupt data right. please advise
Avatar of gudii9

ASKER

>>>this" refers to the object whose method you are inside:

String getName() { return "Hello" ; }

void whatIsThis() {
    // "this" refers to the current object
    System.out.println(this.getName()) ;

    synchronized(this) {
        // Two threads won't try to modify "this" object at the same time.
    }
}

Putting the keyword "sychronized" on the method is just shorthand for writing "sychronized(this)" within the method.




so when you say
are we synchronizing entire object not the individual method. please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

i am trying to understand