• 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

doubt on synchronized block in java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ! .Today i am going to ask on synchronized block.

the general form of synchronized block is


1)Here object means object of any class.i.e we can lock object of any class besides the object of "table" class.
2)if we place this in place of object it is possible to lock object of table class
3)then how can we lock object of a class other than table?can you give example based on below programme?
4) if we want to access a variable or method from table class we need object of table .
if we lock object of table class it is not possible to an object of table class to access synchronized block of table class
from two different places simultaneously.
but if we lock object of another class,say X,how can the object of class X can access synchronized block of table class?
because object of class X is not object of table.object of table only can access members of table.

I think you got my doubt.
I request you to clarify my doubt based on below programme.

==========================

output:

5
10
15
20
25
100
200
300
400
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shankara,

Welcome to CodeRanch!

Please, UseCodeTags. I've added code tags for you this time. Also, please try to write more readable code(using proper indentation etc.).

Now, coming to your problem, technically, with synchronized block, we don't lock an object, but we acquire a lock on an object. What this means is - if a thread is having a lock on an object(that is - enters in synchronized block), then no other thread can acquire lock on the same object unless the first thread releases the lock(that is - exits from synchronized block).

As stated above, you can acquire lock on any object. E.g. in your code, you are sending object of table class to both threads and you are synchronizing the code on 'this' (that is, current object of table class). You can synchronize on any other object (e.g. a String, an Integer and so on).

Only important thing is - it should be same object to prevent multiple threads entering in same synchronized block.

I hope this helps.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shankara,

The following code may help you with your problem.

 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic