I read one piece of code written by somebody and behold interesting
class as below:
class SomeFramework {
public SomeFramework() {
...
DameonThread dameon = new DameonThread();
dameon.start();
}
private doSomething() {
...
}
private class DameonThread extends Thread {
public void run() {
...
doSomething();
...
}
}
}
As you can see, in the constructor of SomeFramework, one thread is
launched and that thread will call the SomeFramework instance's private
method. Perhaps the author design this code to make the client code
easy. The client just call "new SomeFramework()" and everything is done.
No need to coding like
SomeFramwork framework = new SomeFramework();
framework.lauch();
However, the problem is that it is possible that when the dameon is
started, CPU will go into run and call doSomething() of the instance of
SomeFramework. But, at that time, the constructor is not returned. Is it
reasonable to call a method of a un-instanstized object? Curiously, this
code works well all the time. Luckily, before dameon is started, all
member variables are instantized in constructor, but I really think this
code is ugly and should be avoided.