wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes not at all able to understand what is happening in this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "not at all able to understand what is happening in this program" Watch "not at all able to understand what is happening in this program" New topic
Author

not at all able to understand what is happening in this program

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
I tried all means to debug and understand but nothing is getting into my head

Here in this program there is a static lock, which is a lock on a class, so at a time only lock1 or lock2 can be attained. So i am not understanding the output:


Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Please quote your sources first and where you got this code from.

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
This is from Khalid Mughal book on Threads Chapter
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
First of all I would advise you to modify that while loop because it is an infinite loop with true therefore it will run forever as the condition is always true. Make something like while(i1<3) for example so that you can see a short output of the program with some pattern.

After that you can run the code with commented second thread so that you can see what the first thread can do by himself, after that uncomment the second thread so that both threads can run and you will see how the pattern of the output have changed. There I am sure you will figure out the code steps.

make sure to also make println() instead of print so that you can have a better look.

Cheers!!!
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Ok i did as you told




Now my first problem is this:
Static objects acquire lock on class...am i correct
so in one thread execution either lock1 can be acquired or lock2 because both of them require lock on Class object which can be available to only one object.

I know i am wrong as per the output but i don't know the reason and feel don't know anything...i am very depressed
Please explain the whole scenario about what is happening here.

Thanks a lot
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Static objects acquire lock on class...am i correct


Not true. I think you are confusing a few terms here.

When you synchronize a method, if the method is static, it will use the class object. Otherwise, it will use the instance (this) object.

This is different for synchronized blocks. When you synchronize a block, it will use the object that is specified in the synchronized block. And it makes no difference if you specify this object via a static reference or not.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
ok Great Henry, i really like your inputs..

ok one more thing

i designed this simple program, so that i can understand things
ok i made this simple program to understand it



Now since we have class lock once Thread a1 finishes execution it is only after that b1 starts..
and if we synchronize on object that is the this instance then all threads continue running.

Conclusion: If we synchronized on Class object only one thread can be running at a time
if we synchronize on Object then different threads from different object can be running at the same time.

and if we change like this


then we can see that same object different threads cannot run simultaneously when synchronized on the this instance
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83

First of all, just ignore static in this code: static Object lock1 = new Object(); because it is confusing you more than helping you.
static methods when they are synchronized then they are locked on the class.

Ok, think about it this way. When you lock on the object then only the thread with the lock to that object can access the synchronized methods or block with in that object, but other threads can access NON synchronized blocks or methods at the same time since they don't need lock for those blocks or methods.

# void doIt(){
# synchronized(lock1){ System.out.print(Thread.currentThread().getName()); i1++; }
# j1++;


So by going back to the original code as the snipe shows above, note that i1++ is in the synchronized block while j1++ is outside the synchronized block. This mean that when (or even before) first thread is incrementing i1++ in the synchronized block second thread can be incrementing j1++. This is what that program is trying to show, some concurrency involving 2 threads inside and outside synchronized blocks.

Cheers!!!
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
ok then why am i getting the ouput that always all the variables are equal, since the non-synchronized code can be called by both the threads so it should be possible that the value be different
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Since these 2 threads are calling sleep() method then the locks are not released because sleep() holds on to the lock while sleeping and when the thread wakes up and finishes the synchronized block then it will release the lock.

Try to comment out all try, sleep() and catch in check() method and you will see that you will get different output: ex. i1 2 i2 1 .


Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
ok this makes me understand some things atleast....thanks a lot
 
Consider Paul's rocket mass heater.
 
subject: not at all able to understand what is happening in this program