• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

can thread access the non-synchronized method in a class when other thread hold lock  RSS feed

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a question regarding the synchronization of method..

can a thread access the non-synchronized method of an instance of class
when the other thread have the lock on the same instance of class?

because as i know the lock is for object ,
so if a class contains both synchronized and non synchronized methods , and if one thread has a lock on object(instance) , is that possible for other thread to call the non synchronized method using the same instance?

please explain it any one.......

Thanks,
vinay rajnish
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. What further explanation is necessary? You've already got the idea. If a method is unsynchronized, then another thread can call it any time, with no special protection.
 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

i am asking this because the non-synchronized method is in the same instance of the class , on which the other thread has the key(lock) with it.

can it be possible to access that non-synchronized method from other thread?

Thanks,
vinay rajnish
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim Yungist,
That's cleard my doubt....
Thanks man!

Thanks,
vinay rajnish
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem, Mr. Rajinush.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by vianyrajnish rajnish:
....can it be possible to access that non-synchronized method from other thread? ....



The problem, if any, is keeping the data in in instance in a consistent state while two or more threads are active in the instance. I just went through a very convoluted discussion in which I figured all this Thread stuff out. Look at it this way:


It may seem odd, but on a multiple processor machine, the two threads can actually be at the same point at the same time. If that is the case, a variable on the left of the assignment operator can be changed by Thread_1 before Thread_2 can read it. Sounds incorrect, but on a Hydra with a theoretical peak performance of 4.8 Teraflops, total memory of 1.2 Terabytes, and a 21 Terabyte DDN disk array, keeping up with Who's on First can resemble Titanic in 30 seconds, re-enacted by bunnies.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!