views:

256

answers:

8

By one line I mean at most 100 chars per line.

(I basically need this to keep the program alive. The main thread registers callback listeners that are run in separate threads. I just need the main one to hang forever and let the other threads do their work)

A: 
for(;;);

But it's very unlikely that hanging the thread is what you want. Instead, you should consider options like joining on the other threads.

Matthew Flaschen
which will do wonders for your CPU usage :-)
rsp
@rsp, true. This is how to hang the thread, but it's very unlikely the OP actually wants to do that.
Matthew Flaschen
+5  A: 

Use executor. By using method shutdown() you'll force the executor to wait until all threads are finished.

nanda
I agree, you should use the new concurrent package: http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/concurrent/package-summary.html
davyM
then the main porgram will not 'hang' forever
Redlab
I bet the meaning of 'forever' that OP means is that the main program wait until all threads are finished. I might be wrong :)
nanda
@nanda, I think you're wrong. It sounds like the OP wants to create a service that listens for messages and does something literally forever. An exit condition probably isn't even in the design.
Tim Bender
+8  A: 

There are a few things you could do that would be better than hanging the initial thread forever:

  1. Use otherThread.join(). This will cause the current thread you are running in to sleep until the other thread has finished executing.
  2. As @nanda suggests, use ExcecutorService.shutdown() to wait until a pool of threads has finished.
  3. Use otherThread.setDaemon(false) and simply let your initial thread exit. This will set your new threads as user threads. Java will not shut down until the only threads running are daemon threads.
krock
sorry if I'm wrong but using join() means that there is only two threads right? main program and listener. Whereas the OP wants listeners.
nanda
@nanda, the assumption (probably correct) is that the OP wants listeners so he can be notified when threads have finished executing whilst the main thread is in something like a `sleep()` loop waiting for this to happen. The extra callback work is not necessary if you use any of the answers given.
krock
Just loop over your threads and call join() for each one. Once one finishes the loop will join the next one. When your loop exits you can be sure that the last thread has exited.
Chris Nava
+4  A: 
synchronized(this) {
    while (true) {
        this.wait();
    }
}

(thanks to Carlos Heuberger. Exception handling omitted in the above code)

This will make the current thread wait on the monitor of the current class until someone calls notify(), or forever.

Bozho
+1 - but beware of spurious wake-ups: you need a forever loop around the wait since it can spuriously (that is, for no reason) return. Catching InterruptedException also needed... it's a bigger one-liner, but probably the best (correct) solution
Carlos Heuberger
I don't like this answer simply on the basis that you aren't actually waiting for anything. (still not down-voting though)
Tim Bender
@Tim Bender - I agree. I answered instinctively to "how to _hang_ a thread". :)
Bozho
A: 
public static void main(String[] args) {
    Thread t = new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
            }
        }
    };
    t.setDaemon(false);
    t.start();
}
Steve McLeod
CPU load near 100%, I guess?
Andreas_D
with Thread.sleep I would say that the CPU is nearer at 0%
Xavier Combelle
+3  A: 

You can use thread.join to wait for all of the threads.

SB
+2  A: 

With a CountDownLatch you can wait untill the count down reached 0, if you make sure it never counts down, maybe only when it needs to end. (This also result in 0% cpu, the opposite of loops that will run forever, and with join() your app will still finish when all other threads finished, The option of the executor is better, but will also end when all executed task have finished)

Redlab
A: 

Thread.sleep(Long.MAX_VALUE);

Ok, so it isn't forever, but talk about a really long time :)

Tim Bender