aspose file tools*
The moose likes Beginning Java and the fly likes Threading synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Threading synchronization" Watch "Threading synchronization" New topic
Author

Threading synchronization

Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
Hi all,

Please check the following code:



Despite of the method callMe being synchronized, when I run the program, I get the following output:



I would expect one of the threads to enter the callMe() method and stall it, so that the second thread cannot enter it at all! Can someone please help me understand the output? Also, if there is something wrong here, how do I correct it so as to allow only one thread to enter the method?

Thanks,
NP


Fiona
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Both MyThread instances synchronize on itself - their synchronization is separated.

The following will work as you expect:
This is because the same MyThread instance is used (as a Runnable) in two different threads.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
Hi Rob,

I tried doing that. But my output is as follows:

FirstThread
FirstThread
FirstThread
FirstThread
FirstThread
FirstThread
FirstThread
SecondThread
SecondThread
SecondThread
SecondThread
SecondThread
SecondThread

Why does the Second thread enter the method at all when it is a synchronized method and when there is an infinite loop within it!

NP


Rob Prime wrote:Both MyThread instances synchronize on itself - their synchronization is separated.

The following will work as you expect:
This is because the same MyThread instance is used (as a Runnable) in two different threads.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

That's odd. I tried it myself just now, and after about 12 seconds I aborted without a single "SecondThread" - after almost 1 million "FirstThread"s.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
it might be something with the thread schedular


Duct tape is like the Force. It has a dark side, a light side, and it binds the whole universe together.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Rob's code worked for me.

Thread scheduling shouldn't have anything to do with it, since locking is involved.


SCJP 6 || SCWCD 5
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
there are never any gaurentees when it comes to the schedular
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Correct; you can't guarantee which of the threads fires first. However, the synchronization should enforce you either get only "FirstThread"s or only "SecondThread"s - not both.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
Very true but your output also depends on the JVM.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Cody Long wrote:there are never any gaurentees when it comes to the schedular


Yes, but there are limits on what can actually happen. It doesn't matter how bad the scheduler is... schedulling a thread that doesn't own the lock to execute code that requires the lock, doesn't make the thread execute the code -- a thread that is in a blocked state will just release its timeslice.

Very true but your output also depends on the JVM.


Again true. But that doesn't mean that there is a JVM out there that doesn't honor synchronization locks -- at least one that has been certified as a valid JVM.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading synchronization