wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes main app won't stop when all threads are done running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "main app won Watch "main app won New topic
Author

main app won't stop when all threads are done running

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I have a class that extends Thread. The main method of this class just starts a thread which starts other threads. The number of threads varies based on a "properties" file. When all of the threads complete, I want the program to stop. I'm already calling join on the original thread started by main and interrupt on the other threads. It works perfectly when the app runs cleanly. Yet when the application throws a NullPointerException (I don't know about the case of other exceptions) in one of the child threads, the main app does not stop.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

Not sure if there is enough information here as it could be a number of things... for example...

While a NPE can unwind the stack of the child thread and effectively release any locks it held, the state of the data may be in a state the other threads can't handle (and may go into a state where they don't return)

You may be using some sort of flag, which is expected by other threads to be set to a value, which won't be done if the thread that is processing exits unexpectedly.


As for interrupt, that only works if the child threads are designed to receive them and act accordingly. If it isn't designed to check, all you get is more bugs.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

BTW, you can also take a thread dump of the JVM, once the NPE occurs, to see just what threads are still running -- and where they are running.

Henry
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I got a thread dump, but I'm not sure how helpful it is. They all seem to be sys_thread_t and most are in a wait state. Is there something I'm missing here?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
While a NPE can unwind the stack of the child thread and effectively release any locks it held, the state of the data may be in a state the other threads can't handle (and may go into a state where they don't return)

I'm not sure I'm following you here. What sort of state could the data be in (for example) that would cause this?

You may be using some sort of flag, which is expected by other threads to be set to a value, which won't be done if the thread that is processing exits unexpectedly.

Possibly, but I don't think so. At least if that's the case, it's not obvious to me.

As for interrupt, that only works if the child threads are designed to receive them and act accordingly. If it isn't designed to check, all you get is more bugs.

Again, this could be, but it's not obvious to me. I'll keep looking.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why do you need the join in the main thread?

What are the BoundedLinkedQueues waiting for?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

Marilyn,

It looks like you are using Doug Lea's concurrent library. Maybe a threadpool (or some other type of executor). Something related with the message queues.

I have practically *no* experience with this library (as I wrote my own with Java 1.4 and eariler), but I am guessing that NPE is messing something up with that library. Those threads are not terminating, and hence, the main thread is not exiting.


Don't really have an answer here... except maybe look into preventing that NPE from happening, or finding some way to catching it earlier so that it can cleanly exit, and not corrupt something higher up in the stack.

Henry
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Ilja Preuss:
Why do you need the join in the main thread?

What are the BoundedLinkedQueues waiting for?


I think his idea was to join all the threads before main was allowed to complete, but it's not working that way.

BoundedLinkedQueues are message queues. They're not threads, so I thought it was IO blocking.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Henry Wong:
It looks like you are using Doug Lea's concurrent library. Maybe a threadpool (or some other type of executor). Something related with the message queues.

I have practically *no* experience with this library (as I wrote my own with Java 1.4 and eariler), but I am guessing that NPE is messing something up with that library. Those threads are not terminating, and hence, the main thread is not exiting.


We're using the EDU.oswego.cs.dl.util.concurrent package with java 1.3. But thanks for all the info. It's been very helpful.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Marilyn de Queiroz:
BoundedLinkedQueues are message queues. They're not threads, so I thought it was IO blocking.


Well, from their Javadocs I get that they are using some "help monitors", from which I assume that they are using threads internally.

I also don't think that Object.wait is used in IO blocking.

What does the stacktrace of the NPE look like?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main app won't stop when all threads are done running
 
Similar Threads
Misbehaving User Threads
Patrick Naughton and Threads
a.run() and a.start()
Kill program once a thread has completed?
Demon Thread