• 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

Java Thread safe class clarification

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

Let consider the following use case,

it blocks on only the caller threads, if two different threads are called then they won’t get blocked, it is as good as no synchronization right ?

same thing is with built in Thread safe classes of java, hence we should not rely on them, we should handle synchronization ourselves right ?
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
Please don't get me wrong but it's really difficult for me to parse whatever you have said.try to use the plain english.don't worry about things like(poor english or anything else,even i am not a native english speaker).you can put your code,pictures,links in the post(that may help us to know what did you intended to ask).

Praveen
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Chandra,

let's call your class A instead of a, to bring it in line with the Java naming conventions.

You are right, that when you have two different A objects, that there is no synchronization between them. But then again, since each object has it own ArrayList, there is no need for synchronization, is there?

But have a look at the class 'Testertje':

Can you comment on this?
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If two threads are locking on same object then they will block each other like on different objects they won't.



 When we shift the synchronization to method level.
 in case of 1. it will block as synchronized(this)  where this=a which is only one instance
 in 2. it will not block as this=a and this=b for two different Threads


 
 
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

Chandra shekar M wrote:If two threads are locking on same object then they will block each other like on different objects they won't.

 When we shift the synchronization to method level.
 in case of 1. it will block as synchronized(this)  where this=a which is only one instance
 in 2. it will not block as this=a and this=b for two different Threads



Looks correct to me.

Henry
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However your conclusion in your original post:

Chandra shekar M wrote:same thing is with built in Thread safe classes of java, hence we should not rely on them, we should handle synchronization ourselves right ?



That doesn't follow at all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic