This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

please explain me why this program is not going into deadlock scenario

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



[HENRY: Added Code Tags]
 
author
Posts: 23909
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

Saikrishna Vemuri wrote:



Your two threads are using different locks.... two threads with four locks (two locks each).

Henry
 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhh.. Okay
but now i 'll modify it as

Lockclas1 l1=new Lockclas1();
Lockclas2 l2=new Lockclas2();
new deadlock1(l1,l2);
new Deadlock(l1,l2);

now can i say the program is in deadlock
Thanks in Advance
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishna Vemuri wrote:ohhh.. Okay
but now i 'll modify it as

Lockclas1 l1=new Lockclas1();
Lockclas2 l2=new Lockclas2();
new deadlock1(l1,l2);
new Deadlock(l1,l2);

now can i say the program is in deadlock
Thanks in Advance



I don't know.

It seems you're overcomplicating it. I don't even want to read your code because a) there's too much of it, b) the indenting is inconsistent and makes it hard to read, and c) the above described change doesn't really make sense, and d) I don't want to have to try to imagine your code as some baseline with some particular change.

If you want to demonstrate deadlock, here's a simpler program that does it:

 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your program is too Simple . i agree with you i've made it look like a complicated program
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishna Vemuri wrote:Your program is too Simple .



Too simple in a good way or in a bad way?

If a bad way, in what way does it not meet your requirements? What problem are you still trying to solve or what question do you still have?
 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey nthng lyk . you program absolutely met my requirements
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishna Vemuri wrote:hey nthng lyk . you program absolutely met my requirements



Great! Glad I could help.
 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops i got struck in middle
i need a clarity .. when Thread-0 Object is executing in synchronized area how Thread-1 got access to it? I heard that sleep() doesn't release d lock is it so ? then how it is possible for Thread-1 to get access to other object lock area?

Dont mind of my doubts i'm just a beginner .!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishna Vemuri wrote:Oops i got struck in middle
i need a clarity .. when Thread-0 Object is executing in synchronized area how Thread-1 got access to it? I heard that sleep() doesn't release d lock is it so ? then how it is possible for Thread-1 to get access to other object lock area?



I'm not quite sure what you're asking.




Are you asking how we can have both output 1 and output 2, since output 1 indicates a lock has already been acquired? If that's your question, then the reason is the same reason your original code didn't show deadlock: T0 is getting the lock for the A object and T1 is getting the lock for the B object. You need to understand that syncing doesn't mean "No other thread can enter any sync block or method." Rather, it means "No other thread can enter any sync block or method that syncs on the same lock I currently hold. Each object has its own lock.

OR...

Are you asking how we can get to output 3, since T0 already holds the lock for object A? If this is what you're asking, then the answer is to look closely at the code and note that that line is printed out right before we enter the sync block. And note that output 3 and output 4 are where the threads get stuck. They never get to "T1 GOT IT A" and "T0 GOT IT B" because of the deadlock.

Remember, the conditions for a deadlock are 1) two (or more) different threads each hold a resource that the other one wants, and 2) neither thread can release the resource it currently holds until after it obtains the one the other one holds.

 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm.. my doubt is related to first paragraph and you made it clear that " two threads on same object " .. Thankq
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishna Vemuri wrote:hmmm.. my doubt is related to first paragraph and you made it clear that " two threads on same object " .. Thankq



You're welcome.

So, just to be clear, you understand that each Object has its own lock?

And you understand that whenever the keyword synchronized appears, that tells our code to acquire the lock for exactly one object (waiting until the lock is released if another thread currently holds it), and has no relation to any other locks?

 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya.. from mrng onwards i'm fighting over it.. getting confused . And now you made it clear its Object level lock,
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify Krishna, there is no such thing as object level lock or class level lock.

JLS -

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:Just to clarify Krishna, there is no such thing as object level lock or class level lock.

JLS -

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.



Right. It's simply that every object has a lock associated with it. Obtaining that lock (by entering a sync block) doesn't "lock the object." It just prevents other threads from obtaining that same lock. The other threads are still able to do whatever they want with that object that's not explicitly synchronized on that lock.
 
Saikrishna Vemuri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

JLS -

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.




Then why the words class level and Object level came into existent ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishna Vemuri wrote:
JLS -

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.




Then why the words class level and Object level came into existent ?

Those phrases don't actually mean anything. There are no different "levels" of locks. The phrases probably came into existence from people thinking that a static synchronized method was somehow different from a non-static synchronized method. For get about phrases like "class level lock" and "object level lock"--there is no such thing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic