This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jacob Sonia
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please quote your sources first and where you got this code from.

 
Jacob Sonia
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from Khalid Mughal book on Threads Chapter
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jacob Sonia
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this makes me understand some things atleast....thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic