• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What Happens If a thread Can't Get the Lock?

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



Hi there!

Threads calling non-static synchronized methods in the same class will only block each other if they're invoked using the same instance. That's because they each lock on 'this' instance, and if they're called using two different instances, they get two locks, which do not interfere with each other.









Is 1 & 2 are two different instances or same instances?

Thanks!
regards
samura

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

I'm not 100% sure i understand your question. When you use the "new" keyword you are instantiating a new instance of an object. So //1 & //2 are different instances.

With regards to the lock on dostuff... first of all your class doesn't compile (pubic == public) second you must implement the run() method (not the Run() method) and third you are not try to access do stuff in this program. To do so you need to do one of 2 things:

1. Call the run() method from the class, this will cause it to be a regular method call and no new threads will spawn.
2. You can wrap your class with a Thread object and call the start() method on that object (see Sun guide), this will span 2 separate threads that will call dostuff synchronously (because you used the synchronized key word on the method) so there won't be a deadlock or a race condition....

hope that helps...
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow Noam, your answer is impeccable

Murali...
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks! & sorry for the confusions....
Here are my understanding..




 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Thread 1 and 2 will synchronize. whereas thread 3 is independent.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

Perhaps write some code in the run method that needs some time when you test synchronization. And also (just an idea) add a yield() to give other threads a chance. That might look silly, as the synchronized keyword simply would not allow the yielding. But it is then more interesting to compare the behavior of the code with / without the synchronized. Again: Just an idea.


And:
Noam wrote:

With regards to the lock on dostuff... first of all your class doesn't compile (pubic == public)



Welcome to the Ranch, Noam!


Yes, all interface methods have to be public. But for that pubic things, the private keyword would be more appropriate...
(Sorry couldn't resist )

samura babu:

Here are my understanding..


I think, you got it!



Yours,
Bu.
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic