user1564815
user1564815

Reputation: 11

hawtdispatch: a simple threadpool example

I am very new to hawtdispatch and want to run a sample program for demonstrating threadpool. Below is the program I try to run:

public class DispatchQueueT {

public static void main(String[] args) {

    DispatchQueue queue = createQueue("Your queue");

    queue.execute(new Runnable()
        {
            public void run()
            {
                for (int i = 0; i < 100000; i++)
                {
                    System.out.println("First Task"+ "time  "+new Date()+" count : "+i);
                }
            }
        });

}

}

But the problem is that instead of printing to the console 100000 times it ramdomly prints only 700 or 800 times and the program stops .. I have no idea how to achieve this simple threadpooling using HawtDispatch .. :(

Also I need to know the answer of some questions

When using hawtdispatch, Is there any way I can know the no of threads being used by the system at a particular time? How is the memonry management of dispatch queues done ?

Please help !!

Thx ... Richa !!

Upvotes: 1

Views: 1102

Answers (1)

LordOfThePigs
LordOfThePigs

Reputation: 11300

I do not know this hawtdipatch library, but this seems to be a typical issue of deamon vs normal threads.

According to the java specification, the JVM exits when all the non-deamon threads have terminated. The thread that executes your main() method is a non-daemon thread, and in a command line application, is also the only non-deamon thread that is running. Your DispatchQueue is most likely spawning deamon threads, which do not keep the application alive.

As a result, the JVM exits soon after it has executed the "queue.execute()" call. Until the JVM is completely stopped, it is possible that the thread in the DispatchQueue has had time to execute a few of your loops, but it does not have time to complete all 100'000 of them before the JVM stops all threads.

After some mucking around in the source code of HawtDispatcher, it turns out that this is indeed the problem. It lies in SimplePool.createWorker()

private SimpleThread createWorker(int index) {
    SimpleThread w;
    try {
        w = new SimpleThread(this);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    w.setDaemon(true); // <- That's the one that causes trouble for you
    w.setPriority(priority);
    w.setName(name + "-" + (index+1));
    return w;
}

It also seems that this behavior cannot be easily altered, because HawtDispatch never allows you to easily substitute your own WorkerPool implementation.

To solve your problem, you need to wait until the task has finished executing, which you can do in two different ways:

1). Add a Thread.sleep() call after queue.execute(). That's simple, but a bit ugly and brittle.

2).Properly wait for the termination of the task by using the proper synchronization tools, such as CountDownLatch

public static void main(String[] args) throws InterruptedException {
    DispatchQueue queue = createQueue("Your queue");
    final CountDownLatch latch = new CountDownLatch(1);
    queue.execute(new Runnable(){
            public void run(){
                for (int i = 0; i < 100000; i++){
                    System.out.println("First Task"+ "time  "+new Date()+" count : "+i);
                }
                latch.countDown();
            }
        });
    latch.await();
}

Upvotes: 4

Related Questions