eclipse的Job类schedule方法会调用run方法吗?源码分析
时间: 2023-08-11 22:07:53 浏览: 52
是的,Job类的schedule方法会调用run方法。我们可以从Eclipse源代码中找到相关的实现。
在`org.eclipse.core.internal.jobs.JobManager`类中的`doSchedule(Job, long)`方法中,会创建一个`JobThread`对象,并将其加入到`JobManager`的线程池中:
```
JobThread thread = new JobThread(job);
thread.setSystem(job.isSystem());
thread.setPriority(job.getPriority());
thread.setDaemon(true);
thread.start();
```
`JobThread`是`Thread`的子类,其实现了`Runnable`接口。在`JobThread`类中,会调用`Job`对象的`run()`方法:
```
try {
if (!monitor.isCanceled())
job.run(monitor);
} catch (Throwable e) {
// handle the case where the job throws an exception
} finally {
job.done(monitor);
if (monitor != null)
monitor.done();
}
```
因此,可以看出,当我们调用Job类的schedule方法时,它会在后台创建一个线程,并在指定的延迟时间后启动该线程,并调用run方法。在run方法中,我们可以编写我们需要执行的任务。