• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Question on Locks

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code when run

class A extends Thread {
String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {System.out.print(sa[0] + sa[1] + sa[2]);}
}}
class B {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {
Thread t1 = new A(sa); t1.start();
sa[0] = "A"; sa[1] = "B"; sa[2] = "C";
}}}

produces the output "ABC"

The explanation given for this says as follows :

"The block inside the main method is synchronized on the String array object sa. Inside the block, a new thread t1 is started and will run at the discretion of the thread scheduler. The A.run method also contains a block that is synchronized on the String array object sa. Even if the thread scheduler moves thread t1 into the Running state, it will block while attempting to acquire the lock of the String array object sa. Thread t1 will continue to block until the synchronized block in the B.main method runs to completion. At that time, the contents of the String array object have all been updated."

But i didnt understand any thing from this.

Could someone please explain me in detail on how this happens ?

Thanks in advance. Srinu
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main thing to understand is...

Whenever two threads synchronizes on the exact same object, the first will acquire the lock, while the second will have to wait, until the first releases the lock, before it can acquire the lock.

So...

The block inside the main method is synchronized on the String array object sa.

The main thread acquire the lock (using the string array) -- even before the "t1" thread created.

Inside the block, a new thread t1 is started and will run at the discretion of the thread scheduler. The A.run method also contains a block that is synchronized on the String array object sa.

The t1 thread is created and started. And somewhere in the t1 thread it will try to acquire the lock of the exact same array object.

Even if the thread scheduler moves thread t1 into the Running state, it will block while attempting to acquire the lock of the String array object sa. Thread t1 will continue to block until the synchronized block in the B.main method runs to completion.

t1 thread may run immediately -- it may not... but if it does run, when it tries to acquire the lock of the string array object. it will wait until the main thread releases it.

At that time, the contents of the String array object have all been updated.

When the main thread releases the lock, the elements in the array has already been changed.

Henry
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even the synchronized() is called on two references, both these reference varaible refer to the same object.
So when on block acquires lock over the object, then the any other block requesting the same object's lock has to wait until the lock is released.

Here synchronized(sa) block inside main method obtains the lock on sa[] array object. It creates a Thread and calls it, but the run method also tries to get lock on same array object.
So it waits until the lock is released by the main() method.
By the time lock is released in the main method the variables are changed and the value ABC is printed
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic