8

I am a new to concurrency and threads - and have been using them when developing my application at work. Essentially i have a number of threads in my RMI application (server side component) that poll changes in files (these files update every few seconds) .

When testing on a dev box I have been running the server from the command line and then closing it manually when finished and rinse and repeat throughout the day.

As it transpires - I think my threads may not be stopping when i shut the command line and still carry on processing. This is leading to some very bad side effects - although I am not 100% sure if this is possible so hopefully someone can confirm this might be the case.

If i make a thread a daemon - does this mean that when i shut the command line - these threads will automatically stop? I need some way of terminating the application nicely but because the server will eventually be run by autosys I am not sure whats the best way to make all threads finish when shut down

Thanks

octosquidopus
  • 3,517
  • 8
  • 35
  • 53
Biscuit128
  • 5,218
  • 22
  • 89
  • 149
  • this question has a slightly different slant and also has another question inside the body - i would really appreciate it if it was not closed – Biscuit128 Mar 17 '13 at 17:28
  • Your main question arises out of a lack of understanding of what "daemon" means for Java, thus the link to the duplicate question. As for your other question (I assume it's the one about safely shutting down your application), it should be asked separately since it is a separate question. – Jeffrey Mar 17 '13 at 17:34

3 Answers3

1

The following code demonstrates creating a thread pool with a dummy running task, that task is then started and when the application is terminated, a shutdown hook runs which cancels all running tasks and cleanly closes the thread pool.

You do not have to use Callable's as I have, you can use Runnables and the threadPool.execute method and simply terminate the threadPool which is a little less elegant.

    final ExecutorService threadPool = Executors.newCachedThreadPool();
    final List<Future<Void>> runningTasks = new ArrayList<>();
    Future<Void> task = threadPool.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            int count = 0;
            while(true) {
                System.out.println(++count);
                Thread.sleep(1000);
            }
        }
    });
    runningTasks.add(task);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            for(Future<Void> runningTask : runningTasks) {
                runningTask.cancel(true);
            }
            threadPool.shutdownNow();
        }
    });

Shutdown hooks are for standalone applications. In a Java EE container, you could do the same with a javax.servlet.ServletContextListener

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
JayTee
  • 1,209
  • 9
  • 15
  • thanks for the advice, does this mean i will have to use the servlet context listener opposed to the shutdown hook? is this much of the same type of thing? – Biscuit128 Mar 17 '13 at 17:48
  • In a standalone Java application you can use a shutdown hook, in a Java web application you use the ContextListener. Depends if you are cleaning up threads on client or server. – JayTee Mar 17 '13 at 17:51
  • yes all threads will need to be stopped. – Biscuit128 Mar 17 '13 at 17:53
1

Threads run inside a Java Virtual Machine. If you stop the JVM, the threads don't run anymore. You could see exiting the JVM as pulling the plug off your computer: nothing can run anymore.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

The RMI Runtime creates threads that you cannot stop. The only way to shut down the service is with a System.exit().

I do this in a separate thread. After: new ShutThread().start(), the shut down thread waits 2 seconds to give any lingering messages a chance to complete the journey then issues the System.exit().

edharned
  • 1,884
  • 1
  • 19
  • 20
  • so for a server that is started from the command line - with no arguments using java -jar server.jar - once this is running how do we then pass a message to it telling it to exit gracefully? – Biscuit128 Mar 17 '13 at 17:42
  • Good point. I write server software so including a method to shutdown is standard practice. If others don't allow this feature, then perhaps they should. – edharned Mar 19 '13 at 13:44