Speed of Synchronization vs Normal

Posted by Swaranga Sarma on Stack Overflow See other posts from Stack Overflow or by Swaranga Sarma
Published on 2011-02-10T06:43:20Z Indexed on 2011/02/10 7:25 UTC
Read the original article Hit count: 109

Filed under:
|

I have a class which is written for a single thread with no methods being synchronized.

class MyClass implements MyInterface{
//interface implementation methods, not synchronized
}

But we also needed a synchronized version of the class. So we made a wrapper class that implements the same interface but has a constructor that takes an instance of MyClass. Any call to the methods of the synchronized class are delegated to the instance of MyClass. Here is my synchronized class..

class SynchronizedMyClass implements MyInterface{
//the constructor
public SynchronizedMyClass(MyInterface i/*this is actually an instance of MyClass*/)
//interface implementation methods; all synchronized; all delegated to the MyInterface instance
}

After all this I ran numerous amounts of test runs with both the classes. The tests involve reading log files and counting URLs in each line. The problem is that the synchronized version of the class is consistently taking less time for the parsing. I am using only one thread for the teste, so there is no chance of deadlocks, race around condition etc etc. Each log file contains more than 5 million lines which means calling the methods more than 5 million times. Can anyone explain why synchronized versiuon of the class migt be taking less time than the normal one?

© Stack Overflow or respective owner

Related posts about java

Related posts about synchronization