learn java
The moose likes Threads and Synchronization and the fly likes can thread access the non-synchronized method in a class when other thread hold lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "can thread access the non-synchronized method in a class when other thread hold lock " Watch "can thread access the non-synchronized method in a class when other thread hold lock " New topic
Author

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

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
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

Joined: Jan 30, 2000
Posts: 18671
Yes.
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
Hi Jim Yungist,
That's cleard my doubt....
Thanks man!

Thanks,
vinay rajnish
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No problem, Mr. Rajinush.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
Have you tried LearnNowOnline? http://www.learnnowonline.com/
 
subject: can thread access the non-synchronized method in a class when other thread hold lock