Java计时器问题可能涉及将它们存储在列表中

时间:2012-04-17 19:51:54

标签: java multithreading list memory-management

因此,我将尝试尽可能一般地讨论我的程序的细节,同时仍然得到一般性的观点。我有两个重要的对象,JobManager和JobWorkers。 JobWorker有一个任务,它应该以固定的间隔执行,直到完成为止。我会忽略工作完成后会发生什么,因为它们永远不会启动。我看到JobManager的计时器正在设置,但JobWorker计时器没有启动。下面是一些伪代码,可以帮助您了解情况。

public class JobWorker
{
    private String jobName;
    private Timer timer;
    private Calendar startTime;

    public JobWorker(String jobName, Calendar startTime, otherFields)
    {
        this.jobName = jobName;
        this.startTime = startTime;
        this.timer = new Timer("EmailJob" + jobName + "-timer");
        //set other fields
    }

    public void start()
    {
        timer.schedule(new TimerTask(){

            public void run()
            {
                //Do stuff - this is where nothing is happening
            }

        }, 0, 60000);
    }

    //Equals method and other stuff
}

public class JobManager
{
    private List<JobWorker> activeWorkers = new ArrayList<JobWorker>();
    private List<JobWorker> pausedWorkers = new ArrayList<JobWorker>();

    private Timer timer;

    public JobManager()
    {
        //loads workers from database

        timer = new Timer("JobManager-timer");

        loadJobs();

        timer.schedule(new TimerTask(){

            public void run()
            {
                //I see this statement every minute as expected
                logger.info("EmailJobManager timer run started.");

                for(JobWorker worker : pausedWorkers)
                {
                    Calendar c = new GregorianCalendar();

                    //I can see worker.getStartTime() in client gui so I know that they have already passed
                    if(c.compareTo(worker.getStartTime()) > 0)
                    {
                        //Never see these log statements
                        logger.info("Memory Address in manager timer loop 2 for " + worker.getEmailJobName() + " ({})", worker.toString());

                        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy - HH:mm");

                        logger.info("Comparing current time ({}) to JobWorker " + worker.getJobName() + " start time ({})", sdf.format(c.getTime()), sdf.format(worker.getStartDate()));

                        activeWorkers.add(worker);
                        pausedWorkers.remove(worker);
                        worker.start();
                    }
                }
            }

        }, 0, 60000);
    }

    public void addWorker(necessary fields)
    {
        //Create JobWorker newWorker = new JobWorker(jobName, blah blah)
        pausedWorkers.add(newWorker);
    }   

    /**
     * Checks to see if any jobs were not completed as expected due to hardware error. Recreates the EmailJob and EmailJobWorker responsible.
     */
    private void loadJobs()
    {
        String SQL = "SELECT * FROM email_jobs";

        List<Map<String, Object>> rows = jt.queryForList(SQL);

        for(Map<String, Object> row : rows)
        {
            JobWorker worker = new JobWorker(stuff it needs from SQL);

            //I see these debug statements
            logger.info("Worker " + worker.getJobName() + " has been added to pausedWorkers list with memory address ({})", worker.toString());
            pausedWorkers.add(worker);

            logger.info("Job " + jobName + " loaded sucessfully.");
        }
}

我将继续添加一些调试语句,即打印出JobWorker内存地址(本质上是Object.toString),以查看它们的深/浅副本是否有奇怪的东西。有什么想法吗?

编辑: 好吧,我正在完全失去所发生的一切。所以这是完整的课程。

http://ideone.com/hR7et

没有什么敏感的,所以不用担心。

这是我的日志输出:

2012-04-17 16:34:18,548 INFO [http-bio-8081-exec-97] c.c.e.EmailJobWorker [EmailJobWorker.java:98] EmailJobWorker test was created sucessfully. (batchSize, timeInterval, currentId, startTime) (1, 5, 1, 04/17/12 - 15:15)
2012-04-17 16:34:18,552 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:297] Worker test has been added to pausedWorkers list with memory address (EmailJobWorker@7e7df7d)
2012-04-17 16:34:18,552 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:301] EmailJob test loaded sucessfully.
2012-04-17 16:34:18,557 INFO [http-bio-8081-exec-97] c.c.e.EmailJobWorker [EmailJobWorker.java:98] EmailJobWorker test2 was created sucessfully. (batchSize, timeInterval, currentId, startTime) (1, 5, 1, 04/17/12 - 15:15)
2012-04-17 16:34:18,558 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:297] Worker test2 has been added to pausedWorkers list with memory address (EmailJobWorker@349be26c)
2012-04-17 16:34:18,558 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:301] EmailJob test2 loaded sucessfully.
2012-04-17 16:34:18,558 INFO [http-bio-8081-exec-97] c.c.e.EmailJobManager [EmailJobManager.java:117] EmailJobManager timer started sucesfully. (sleepTime) (1)
2012-04-17 16:34:18,559 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:74] EmailJobManager timer run started.
2012-04-17 16:34:18,561 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:85] Size of pausedWorkers: 2
2012-04-17 16:34:18,561 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:89] Memory Address in manager timer loop 2 for test (EmailJobWorker@7e7df7d)
2012-04-17 16:34:19,501 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:34:31,984 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:34:34,562 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:04,840 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:19,501 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:31,984 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:35:34,562 INFO [EmailJobManager] c.c.e.EmailJobManager [EmailJobManager.java:75] EmailJobManager timer run started.
2012-04-17 16:36:04,840 INFO [EmailJobManager] c.c.e.EmailJobManager 

正如您所看到的那样,计时器甚至不会每分钟都关闭,即使EmailJobManager只创建一次,JobWorkers的计时器也永远不会出现。

2 个答案:

答案 0 :(得分:2)

  

我看到JobManager的计时器正在启动,但是JobWorker   定时器尚未启动。

你打电话给JobWorker.start吗?您需要包含更多代码,以便我们了解正在发生的事情。这些run方法没有理由不应该执行:http://ideone.com/uEer3

瞬态仅与序列化有关。

答案 1 :(得分:0)

所以最终我的timertasks的run方法出错了。由于它们是匿名的,因此错误确实导致主方法中断,并且无声地失败。