gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:49:21 27 4
gpt4 key购买 nike

因此,我将尝试尽可能广泛地讨论我的程序的具体细节,同时仍然阐明一般要点。我有两个重要的对象,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 的计时器也永远不会关闭,计时器甚至不会每分钟关闭一次。

最佳答案

I see the JobManager's timer is being set off, but the JobWorker Timers are not being started.

您正在调用JobWorker.start吗?您需要包含更多代码,以便我们可以看到发生了什么。这些 run 方法没有理由不执行: http://ideone.com/uEer3

transient 仅与序列化相关。

关于Java 计时器问题可能涉及将它们存储在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10198159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com