简体   繁体   中英

Does Websphere respect Daemon threads?

I've got an app that creates a load of Daemon threads, I'd like each one to shut down when the app is shut down.

I'm a little worried thought that Websphere 7 might not be shutting them all down.

Does anyone know if Websphere 7 treats Daemons threads differently? (I know it should do)

Note: I know what shouldn't create threads manually, and that I should probably use WebSphere WorkManager or something, but this app has to run in Tomcat and WebSphere.

I know that I should tie in all threads to some context/shutdown mechanism, this is in progress.

Each WAS server runs a single JVM, and daemon threads are tied to the JVM's lifecycle, not the app's lifecycle. Therefore, you should not expect any daemon threads to be shut down when your app stops.

As you've already indicated, you should not create threads manually; the Java EE specs forbid this and the behavior in a Java EE container is different than a standalone Java application as you've already found. Unfortunately, there is currently no Java EE standard for a WorkManager equivalent; however, JSR-236 (Concurrency Utilities for Java EE) may be back as a candidate for inclusion in Java EE 7 .

In the meantime, on WAS, you can use the asynchronous beans ( WorkManager ). We have successfully used this method to tie threads to the application lifecycle.

However, since you need to run in another container as well (Tomcat), there may be some other options to consider handling concurrency in your applications:

Some other potential options for handling concurrency include the following, but these require EJBs, which may not be available in Tomcat:

Here are a few related threads on the topic of concurrency in Java EE:

As has been mentioned you're not supposed to do this, but there isn't a good way to do it. This hasn't caused any problems for me.

This approach requires centralized thread-creation and the use of a listener to terminate threads when the app is stopping.

You'll have to do a few things:

  1. Centralize all thread creation in a single class (call it ThreadService). When a thread is created here put it in a list so you can later loop through the list to stop them all.
  2. Make an interface that your threads implement that allows you to stop each thread via the same interface. Each thread you have has to implement it's own mechanism for handling this. For example if your Thread uses a loop and Thread.sleep() then set stopped=true and interrupt the thread. The loop should check this and break from the loop when stopped=true.
  3. Make a listener and implement ServletContextListener. When contextDestroyed() is called call ThreadService.stopThreads(). Register this listener in web.xml.

Websphere is just a java application. It cannot respect or do not respect deamon threads that are the feature of JVM or java runtime environment. So, if you create deamon thread inside Java EE application it will be deamon in every application server.

Moreover as far as I know even if you create regular thread it will not prevent application server from shutting down: the shutdown mechanism of every application server tries to close all its components and in the end runs System.exit() to win the criminals :) that open threads manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM