Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executor and Daemon in Java

I have a MyThread object which I instantiate when my app is loaded through the server, I mark it as a Daemon thread and then call start() on it. The thread is meant to sit and wait for information from a queue as long as the application is active. My problem/question is this: Currently MyThread is extending Thread because I mark it as Daemon and I read about how it's more prefferable to implement Runnable and to use Executors. So what I wanted to ask is if MyThread will implement Runnable instead of extending Thread (and of course will be renamed) and I'll use newSingleThreadScheduledExecutor() how, what or maybe where, do I mark something as Daemon. I hope I haven't made a mess of terms, please excuse me if I have as some parts of the multithreading environment are very new to me.

Thanks Ittai

Update: The module I'm referring to in my app is a web-app which has a few threads actually of this sort and what they do have in common is that they all in the ServletContext as a member for various reasons. Currently I extend Thread to WebThread which has the ServletContext as a memebr and all subclasses can utilize this. If I switch over to the Runnable paradigm with the Executor and ThreadFactory than basically I need to have an ugly hybrid of WebRunnable which implements Runnable and has the ServletContext as a public member and have my ThreadFactory implement newThread(WebRunnable arg0) in addition to newThread(Runnable arg0). I'm not sure what's best. Thanks

like image 421
Ittai Avatar asked Oct 04 '09 11:10

Ittai


People also ask

What is a daemon in Java?

Daemon thread in Java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically. There are many java daemon threads running automatically e.g. gc, finalizer etc.

What is executor in Java?

Interface Executor An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads.

What is executor and ExecutorService in Java?

Executor just executes stuff you give it. ExecutorService adds startup, shutdown, and the ability to wait for and look at the status of jobs you've submitted for execution on top of Executor (which it extends).

What is daemon problem in Java?

A Daemon thread is a background service thread which runs as a low priority thread and performs background operations like garbage collection. JVM exits if only daemon threads are remaining. The setDaemon() method of the Thread class is used to mark/set a particular thread as either a daemon thread or a user thread.


3 Answers

Check out the JavaDoc for newSingleThreadScheduledExecutor(ThreadFactory threadFactory)

It would be implemented something like this:

public class MyClass { 
    private DaemonThreadFactory dtf = new DaemonThreadFactory();
    private ScheduledExecutorService executor = 
                                 Executors.newSingleThreadScheduledExecutor(dtf);
    // ....class stuff.....
    // ....Instance the runnable.....
    // ....submit() to executor....
}

class DaemonThreadFactory implements ThreadFactory {
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setDaemon(true);
        return thread;
    }
}
like image 68
Stu Thompson Avatar answered Oct 04 '22 23:10

Stu Thompson


If you're using a scheduled executor, you can provide a ThreadFactory. This is used to create new Threads, and you can modify these (e.g. make them daemon) as you require.

EDIT: To answer your update, your ThreadFactory just needs to implement newThread(Runnable r) since your WebRunnable is a Runnable. So no real extra work.

like image 30
Brian Agnew Avatar answered Oct 04 '22 23:10

Brian Agnew


Just to complement with another possible solution for completeness. It may not be as nice though.

final Executor executor = Executors.newSingleThreadExecutor();
Runtime.getRuntime().addShutdownHook(new Thread() {

    @Override
    public void run() {
        executor.shutdownNow();
    }
});
like image 30
Johan Sjöberg Avatar answered Oct 04 '22 23:10

Johan Sjöberg