views:

199

answers:

2

I have developed a small Java application that runs two scheduled threads via a scheduled executor service. On most computers my application runs just fine. In testing however I have come across a computer where my threads are not running as often as they should or not at all. I have one thread scheduled to run on 250 ms intervals. It simply checks to see if there is anything to read on std in, if there is it reads it and executes a command. This thread runs sporadically but never as often as it should. My other thread runs every 5 seconds and simply prints something to the screen. It runs once and then never gets ran again. Here is the code I am using:

    scheduledThreadManager.scheduleWithFixedDelay(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                if(inputReader.ready())
                {
                    String command = inputReader.readLine();
                    executeCommand(command);
                }
            }
            catch(IOException e)
            {
                System.out.println(e.toString());
                e.printStackTrace();
            }
        }
    }, 250, 250, TimeUnit.MILLISECONDS);

    scheduledThreadManager.scheduleWithFixedDelay(new Runnable()
    {
        @Override
        public void run()
        {
            System.out.println(idleString);
        }
    }, 0, 5000, TimeUnit.MILLISECONDS);

I have made sure that my app is not hanging during execution of the scheduled threads. The computer has a Core2Duo processor in it so I can't see how the hardware would not be able to meet my needs, but perhaps that is not so. The other interesting thing is that I am running a main application thread in addition to these and it is running correctly. Any input into what the cause of this problem is would be greatly appreciated.

+1  A: 

There are two ways I think you can approach the problem:

  1. Setup a profiler and watch the applications progress over time until it hangs.

  2. If the application is hung then use kill -3 (Linux) or CTRL-BREAK (Windows) to obtain a thread dump which will give you some more insight into what's going on. They're not the easiest things to read however. See:

http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/

Jon
+3  A: 

You said " I have one thread scheduled to run on 250 ms intervals", but that's not what you've programmed it to do. You used scheduleWithFixedDelay, and that means "leave 250ms between the end of one execution, and the start of the next". So if your task takes 100ms to execute, then it'll be 350ms between executions, and if your task's execution time varies a lot, then so will the intervals at which it gets executed.

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

Have a look at scheduleAtFixedRate instead, that has "run every X millis" semantics, and may be more what you need:

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.

The big danger with scheduleAtFixedRate is that your tasks can end up overlapping, if you configure it wrong, or your tasks take too long to execute.

skaffman
Thanks for the response but I understand the difference between these scheduleAtFixedRate and scheduleWithFixedDelay very well. As stated above in my question I have verified that that my threads are not hanging via print statements, they are simply not executing.
Kam Sheffield