File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HI Help synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HI Help synchronized" Watch "HI Help synchronized" New topic
Author

HI Help synchronized

Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Hi i am starting studying for SCJP and i was testing this code and seems strange for me.. i am using a runnable when i use the runnable interface PRINT method is synchronized but when i dont use Interface runnable and i use run method extending from Thread PRINT is not synchronized using the same Source code.. Sorry by my poor english.

here is the code. Thanks in advance..


Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
It has to do with your Print() methods that you have declared as synchronized. Both methods synchronize on their class instances. You instantiate one MyRunnable instance and then create four Threads. All four threads are synchronizing on the same MyRunnable object so each one waits for the previous one to complete before continuing. You instantiate four instances of MyThreads. Your four threads are synchronizing on four different objects so none are waiting for any other. They are all running together.

BTW, it helps a lot to get answers on this forum when you UseCodeTags. Here's your code when using code tags:
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Synchronizing a method will prevent this method to be called by the same instance from different threads.

Your first version (MyThreads a = new MyThreads();) is making four different instances of a class having the Print method. Each instance is calling its own Print method. There's no synchronization happening here. Your second version (MyRunnable Runnable = new MyRunnable()) uses only one instance having the Print method, and four threads. They all use the same Runnable instance, but as the method of that instance is synchronized, other threads will have to wait before being able to call the method in their turn.


[My Blog]
All roads lead to JavaRanch
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Christophe Verré wrote:Synchronizing a method will prevent this method to be called by the same instance from different threads.


Thanks Christophe i realize i have a long way to go yet. would like to write a sample code to help me.. thanks in advance

this is my first post thanks for the advices.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
thanks a lot Tom. look now i understand perfect my mistake in my previous code.. now what is wrong with synchronized code with Strings. because i use a string literal and synchronized perfect but for a new String Object (Using new string()) not synchronized as well. and i read some time ago strings literal doesn´t work for synchronized blocks what is wrong for synchronized blocks with strings.

thanks a lot.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Christian Daniel Ortiz Cuellar wrote:Thanks Christophe i realize i have a long way to go yet. would like to write a sample code to help me.. thanks in advance

I think that your sample illustrates the problem well enough. One case using a single instance, and one case using four different instances.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Christophe Verré wrote:
Christian Daniel Ortiz Cuellar wrote:Yes In Fact i understand it perfectly thanks a lot.

I think that your sample illustrates the problem well enough. One case using a single instance, and one case using four different instances.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HI Help synchronized