Archive for the 'eng' Category

Spurious Wakeup

admin September 14th, 2008

I have finished reading Effective Java long time ago. It is such a great book. The more I passed through each page the more I realized how little I know about java programming. The distance between “coder” and “developer” is really far

I read most of the item listed in the book. One of the items I have skipped was Item 50: Never invoke wait outside a loop. The code below show the concept of this item

synchronized (obj) {
    while (<condition does not hold>)
        obj.wait();

     ... // Perform action appropriate to condition
 }

Looking at the name of the practice, I thought I knew all the reasons behind it so I just skipped it. Today I found an interesting post asking What is spurious wakeup. I read it and found that “threads can wake up on wait() for no reason at all”!!!!! There are quite many good references for this fact and one of them is, guess what, the item 50 of Effective Java. I would have known it for long time ago if I just read it. One of the reasons behind it stated in the item is that

The waiting thread could wake up in the absence of a notify. This is known as a spurious wakeup. Although The Java Language Specification [JLS] does not mention this possibility, many JVM implementations use threading facilities in which spurious wakeups are known to occur, albeit rarely [Posix, 11.4.3.6.1]

I thought sometimes it was OK to call wait() without condition-checking loop if the object to wait on represented just one condition, the object was shared only between waiting and notifying threads and the code’s execution order guaranteed that wait leaks would not occur. Now giving that JVM implementation can send spurious wakeup signal, the condition checking loop is A MUST

Apparently, the spurious wakeup is an issue (I doubt that it is a well known issue) that intermediate to expert developers know it can happen but it just has been clarified in JLS third edition which has been revised as part of JDK 5 development. The javadoc of wait method in JDK 5 has also been updated

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops

You may wonder (like me) why JLS designer decided to allow this kind of thing to happen. It’s not that I don’t want to use condition checking loop. The checking is the best practice that developers should always do no matter of them knowing anything about the spurious wakeup or not. But I just don’t see the benefit of allowing the wakeup for no reason. It turns out that this is something about performance as stated in Multithread Programming with Java

Due to some arcania in the hardware design of modern SMP machines, it proves to be highly convenient to define them like this. The hardware runs a little faster, and the programmer needs to reevaluate the condition anyway

I just commit a copyright violation

admin September 14th, 2008

The story is that I have read an interesting blog entry of Russell Ball and decided to translate the whole blog entry and put it on my extra blog in which I intend to store my translated articles. A few days later Russell wrote a blog entry which, at first, made me think he didn’t approve my act (sorry for my bad interpretation). He clarified me later that I have misunderstood his meaning. He even allowed me to translate any of his blog entry. Thanks very much Russell.

To make sure about the translation issue, I have gathered more information about copyright law and realized that just making it clear that I am not trying to steal other people’s content by placing the original author’s name in the translated article and providing a link to the original content doesn’t make me able to get away from copyright violation.

I found an article talking about the case of translation and online copyright. The author have simulated the situation that.

1. Person A writes an article in Japanese.
2. Person B sees the article, translates it into English, and posts the translated article on his own website

Is Person B infringing Person A’s copyright?

There are 3 person B’s used cases.

1. Person B posts the translated article on his own website without giving any credit to Person A, or providing a link back to Person A’s website

2. Person B posts the translated article on his own website, and credits Person A as the original author. Better yet, Person B even posts a link Person A’s article.

3. Person B posts the translated article on his own website after obtaining permission from Person A.

My case with Russell’s content falls in number 2 the one which seems OK but it’s not.

The translator may have put real effort in translation and once the original work has been translated, the output can be considered as a “derivative work” of the original source. He even makes his intention clear by giving credit to the author and providing a like to the original content. But as long as the translation is not in “fair use”, he is still subject to copyright infringement.

I have translated the whole blog entry, not a part of it. All parts of the output work derived from the original work so I can’t say that I am showing an excerpt of a blog or I just want to show an example message or I am just trying to make a parody. The output work is not a fair use of the original work.

The article about Japanese translation explains that:

Using our example above, it’s the same as Person B creating his own Gundam series where the Gundam are made out of wood. Although Person B’s wood Gundam are different from Person A’s metal Gundam, Person B is still infringing on Person A’s copyrights.

Things will be different if the original work’s owner is willingly to put his/her work in a license that is friendly for distribution and making derivative work like “Creative Common License”. The best and safest way for this kind of scenario is to ask for the author permission in performing any thing on his/her work.

Now let me tell you a bit about my motivation behind my purpose of translating other people’s articles before I end this blog entry.

I am a software developer and I , like all other developers around the word, trying my best to keep myself up to date with all the technology trends and knowledge. With the extremely fast flow of IT information these days, waiting for a government’s department to translate modern technical books or initiate a project to translate articles about the newest technology to thai is just like an impossible hope.

So I have been training myself to read english so I can learn directly from the original sources. The thing that starts my motivation is that I often recommend some good resource or articles on internet to my colleagues and got back the reply like “Do you have those articles in thai”, “I am not that good at reading English, I will see if I can get it somewhere else in thai”. Useless for me to tell them that being in IT field, reading and writing in english are not something we can avoid. I am not very good at reading/writing in English too but at least I am willing to keep practicing.

Not very long ago I found www.thaidev.org which provides forum/resources for thai developers and encourage people to submit translated article under Creative Common License. I thought, “this is great” I might have something to contribute. My hope is that thai developers may feel more comfortable to read those translated article and , at the same time, develop their interest to seek more of this informative resources from the original sources and gradually become familiar with reading in english.

I hope I will also benefit form the translation process. I should gain more understanding in the article I am working on because I will have to do some crosscheck with other resources and performing some research to make sure I truly understand what I am doing otherwise I will just embarrass myself in front of public place if I have put a false information in the translated work.

Inversion of Control and Dependency Injection are not the same

admin September 14th, 2008

The term IOC (Inversion of Control) and DI (Dependency Injection) have often been used interchangeably. Both concepts have been around in software design for a long time and just have became in spotlight recently due to, I guess, the concept of decoupling software components to increase maintainability , make cleaner code and encourage unit-testing has became more and more popular.

There are many frameworks to help developers to implement the concepts but the most adopted one is Spring framework. The fact that Spring call its dependency injection framework “IOC container” so, with its incredible popularity, people start using IOC as the synonym of DI. Technically, IOC is a software design principle which has a more generic meaning than DI. DI is just a specific form of IOC.

I am not going to pretend to be an expert in software design. If you want to hear from the real expert then I recommend you to read Martin Fowler’s article on the concept of IOC and how it related to DI and just forget about the rest of my blog. The core concept I am going to tell you I derived it from those two articles.

What is Inversion of Control?

IOC is about how software components or areas of code give up its “control” to other components or other areas of code. The “control” may be execution flow, object life-cycle managing, the way subcomponent has been created or any program activity.

Let’s look at an example

ServerSocket echoServer = new ServerSocket(8888);
while(true){
    Socket clientSocket = echoServer.accept();
    ClientHandlerThread handler = new ClientHandlerThread(clientSocket);
    handler.start();
}

The above area of code use standard java API to create a server program. It start by binding socket to port 8888 , put it self in forever loop waiting for client connection. Once it got a connection, it decides to create a thread to handle the connection then get back to while-loop to wait for next request. You can think of the above code as a user program which use java network API as a library. The program controls its own execution flow and manages its own life-cycle (in this case the server stop when administrators manually kill its process). The library can be look as a passive component with the user program actively drives the whole program.

Now, let’s say we want a HTTP server. There are so many details to handle when you deal with HTTP protocol. It’s complicate enough for java as a platform to create a framework for you so you don’t need to get your hand dirty in dealing with low level details.

With Servlet API, you only have to concern about your business logic by specify what you want to do with clients requests in doGet/doPost methods. The API and web container will intercept socket connection can call the method you have implemented.
In this case, the control invert from your code to the framework. The framework will drive the whole program. It will call init() to let you have a chance to initialize your component before accepting request and will call destroy() when it decide your servlet shouldn’t be active anymore. This is way IOC is also known as the Hollywood Principle - “Don’t call us, we’ll call you”.

What is Dependency Injection?

DI is a specific form of IOC. In this case, the “control” that is inverted is the way your component manage its dependency.

class MyComponent{
	private ConfigParser parser;

	public MyComponent(String configFile){
		parser = new ConfigParser(configFile);
	}

	public void init(){
		String debugStr = parser.getConfig("debug");
	}
}

MyComponent internally create ConfigParser in it constructor. This will make MyComponent tightly depend on ConfigParser. What if, in the future, you create another version of ConfigParser which read parameters from a configuration repository server? MyComponent has to come up with a way to choose which implementation to use. Not just MyComponent, all code that explicitly create ConfigParser have to be changed to accommodate a new type of parser.

Another thing is the above code is very hard to do unit testing. The ConfigParser implementation always read parameters form file. Imagine you are writing JUnit test case for MyComponent class with 20 test scenario and you have to create a configuration file for each scenario.

interface ConfigParser{
  public String getConfig(String configKey);
}

class FileConfigParser implements ConfigParser{
  public FileConfigParser(String configFile) {   //init   }

public String getConfig(String string) { //Read Parameter from file. }

}

class RemoteConfigParser implements ConfigParser{
  public RemoteConfigParser(String host, int port) { //init  }

  public String getConfig(String string) { //Read Parameter from remote server. }
}

class MyComponent{
  private ConfigParser parser;

  public MyComponent(ConfigParser configParser){
    this.parser = configParser;
  }

  public void init(){
    String debugStr = parser.getConfig("debug");
  }
}

Now, our MyComponent doesn’t explicitly create an implementation of ConfigParser. The control of how to create ConfigParser has been inverted to be a responsibility of someone else or put it another way, someone will create a concrete implementation of ConfigParser (dependency) and inject it to MyComponent. This dependency injection pattern help promote loosely couple system. MyComponent class doesn’t entirely depend on a specific type of ConfigParser, any parser implementation can be injected to MyComponent becase all implementations fulfill the same contracts specified in ConfigParser interface. In JUnit test case you can create a mock parser which read parameters from a StringReader containing just those parameters for a specific test scenario.

RemoteConfigParser is likely to have its own dependency, let’s say it’s a component encapsulate connection transport. You do the same as you did with ConfigParser- externalize the dependency into Transport interface to make someone else be able to inject concrete transport into RemoteConfigParser. Now what if a transport implementation has a few dependencies?

You can get the picture that now our system comprises of many components with its dependency hierarchy. Before you are able to use a component, you have to resolve its whole dependency hierarchy. In the large system which contains hundred or thousand components, who is going to do that for you.

Now it might not be very hard to guess what Spring IOC container is for. I will refactor our program to make RemoteConfigParser has two Transport dependency. Here is the Spring IOC configuration file which I will used to tell spring how to resolve MyComponent dependency hierarchy.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="primary-transport" class="example.ioc.TCPTransport">
    <constructor-arg value="172.20.12.16" />
    <constructor-arg value="8888" />
</bean>

<bean id="backup-transport" class="example.ioc.TCPTransport">
    <constructor-arg value="172.20.12.17" />
    <constructor-arg value="8888" />
</bean>

<bean id="configParser" class="example.ioc.RemoteConfigParser">
    <constructor-arg ref="primary-transport" />
    <constructor-arg ref="backup-transport" />
</bean>

<bean id="myComponent" class="example.ioc.MyComponent">
    <constructor-arg ref="configParser" />
</bean>
</beans>

Somewhere it my program initialization I will ask Spring to create MyComponent for me.

String configFile = "mycomponent-config.xml";
ApplicationContext appContext = new ClassPathXmlApplicationContext(configFile);  

MyComponent myComponent = (MyComponent)appContext.getBean("myComponent");

I don’t know it is correct or not to call a DI framework a “IOC container” but IOC and DI is not 100% equivalent as you may think.