Friday, June 17, 2005

Simple way to wait in a program or stop a program from exiting.

We often require a main thread of a application to keep waiting (and not end).
For Console applications, we can just block with a Console.Read() method.
For other applications, we tend to write a infinite loop like -- while(true){}
But the above statement keeps on wasting a lot of computational cycles.

A much cleaner and cool approach would be :--

java.lang.Object sync = new java.lang.Object();
synchronized (sync) { sync.wait(); }