Tuesday, December 19, 2006

Funny code

Some time back, I had to write a program where I need to have a thread which continuously looks for some status and does some work in background.
So, I started off like this.
Class MyThread implements Runnable
{
public void run()
{
//Something here

}

}


Class MyClass
{
public void function1()
{
MyThread thread = new MyThread();
thread.run(); // assuming thread runs in background
//do my work here
}
}

And I was wondering, why my program is taking so much time. And then, I figured out, that thread is running and my program is exiting only after thread finishes its work. :)
Later, my teammate asked me "Did you find out where you are wrong? " and explained where I was wrong. :)
Some times, we do such silly mistakes for which we waste hours and hours. :)

2 comments:

Anonymous said...

Hmm.. where is the error?

Pallavi Palleti said...

I should call start() method rather run() method. For that I have to encapsulate my MyThread object in Thread object.