• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization

 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please explain these lines in a simple manner:

When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired.

But, when you synchronize a block of code, you specify which object's lock you want to use as the lock. That gives you the ability to have more than one lock for code synchronization within a single object.

Scjp6, page (736-737)
 
Henry Wong
author
Posts: 23951
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

Manpreet Kamboj wrote:Can anybody please explain these lines in a simple manner:



Can you elaborate what is your confusion? What do you think they mean?

Henry
 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Myrunnable r1 = new Myrunnable();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r1);
t1.start();
t2.start();

I know r1 is the job that workers (t1 & t2) are supposed to perform.Also, if t1 will enter synchronized method, t2 will have to wait and vice versa.

What I don't know is which object we are talking about? Are we talking about getting a lock on object r1?

 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we acquire multiple locks on single object by using synchronized blocks?


Also,if:

Myrunnable r1= new Myrunnable();
Myrunnable r2 = new Myrunnable();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();

Here both t1 and t2 can acquire lock on synchronized method at the same time? Because they will not interfere with one another. Is it so? Here can we say that threads t1 & t2 have acquired locks on different objects?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any locks being acquired here.
Can you show us what Myrunnable os doing?
 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I am just taking an example. Myrunnable is a class that implements runnable interface.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manpreet Kamboj wrote:Well, I am just taking an example. Myrunnable is a class that implements runnable interface.


I have guessed that.
What I meant that it all depends on what Myrunnable.run() contains.
There is no locking on your example. It might be some in Myrunnable.run().
 
Manpreet Kamboj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the run method contains a call to any synchronized method or let say run itself contains synchronized block of code, that accesses let say common resource int bankbalance, then it means bank balance is the object?
 
Henry Wong
author
Posts: 23951
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

Manpreet Kamboj wrote:
I know r1 is the job that workers (t1 & t2) are supposed to perform.Also, if t1 will enter synchronized method, t2 will have to wait and vice versa.

What I don't know is which object we are talking about? Are we talking about getting a lock on object r1?



The object being used as a lock, for a synchronized (non-static) method, is the object referenced by the "this" variable. This is why we are asking you to show us more code. Without knowing which method (and hence, instance) that you are referring to, we don't know which instance the "this" variable is referring to.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic