如标题中所述,我正在尝试使用批处理 IResourceChangeEvents 的方法创建 IMarkers。我的目标是以正确、稳定的方式创建 IMarkers。使用 IWorkspaceRunnable 是否有任何限制(为了批处理更改事件),或者在当前线程中创建 IMarkers 是否更安全?是否有任何可预见的错误,例如并发问题?
这是一个开放式问题,但我只想了解使用 IWorkspaceRunnable 创建 IMarkers 的优缺点。下面是代码示例,有和没有批量更改:
// Generates marker with the given attributes
public static IMarker generateMarker(final IFile file, final Map<String, Object> attributes,
final String markerType) throws CoreException, BadLocationException, IOException
{
if (!attributes.containsKey(IMarker.LINE_NUMBER))
{
// Assumes that attributes has a mapping for IMarker.CHAR_START, which is invariant when creating markers in Solstice
int line = ResourceUtility.convertToDocument(file).getLineOfOffset((int) attributes.get(IMarker.CHAR_START));
attributes.put(IMarker.LINE_NUMBER, line + 1); // lines indexed at 1, not 0
}
IWorkspaceRunnable r = new IWorkspaceRunnable()
{
public void run(@Nullable IProgressMonitor monitor) throws CoreException
{
IMarker marker = file.createMarker(markerType);
marker.setAttributes(attributes);
MarkerField.marker_ = marker; // MarkerField is just an inner class. Functions as a pointer to a pointer.
}
};
file.getWorkspace().run(r, ResourceUtility.getRuleFactory().markerRule(file), 0, null);
return MarkerField.marker_;
}
或者,我可以消除批处理机制,并使用下面的代码:
// Generates marker with the given attributes
public static IMarker generateMarker(final IFile file, final Map<String, Object> attributes,
final String markerType) throws CoreException, BadLocationException, IOException
{
if (!attributes.containsKey(IMarker.LINE_NUMBER))
{
// Assumes that attributes has a mapping for IMarker.CHAR_START, which is invariant when creating markers in Solstice
int line = ResourceUtility.convertToDocument(file).getLineOfOffset((int) attributes.get(IMarker.CHAR_START));
attributes.put(IMarker.LINE_NUMBER, line + 1); // lines indexed at 1, not 0
}
IMarker marker = file.createMarker(markerType);
marker.setAttributes(attributes);
return marker;
}
每种方法的优缺点是什么?